Skip to content

Instantly share code, notes, and snippets.

@kurtkaiser
Last active June 18, 2019 19:51
Show Gist options
  • Save kurtkaiser/78822058358f97847a8c0ddaff589b7d to your computer and use it in GitHub Desktop.
Save kurtkaiser/78822058358f97847a8c0ddaff589b7d to your computer and use it in GitHub Desktop.
/*
Cheat Detector
By Kurt Kaiser
kurtkaiser.us
*/
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import java.io.*;
import java.util.Scanner;
public class DemoCheatDetection {
// Demonstrating the program
public static void main(String[] args) throws IOException {
String fileName =
"raceTextData.txt";
int[][] runnerTimes = new int[3][];
String[] runners; // = new String[3];
int[][] splits = new int[3][2];
System.out.println(" --- Cheating Data ---");
try {
// Create object
RaceDataLog recent = new RaceDataLog(fileName);
// Calling methods
// Checking for skipped sensors
runners = recent.skippedSensor(fileName);
if (runners[0].equals("cheated")) return;
// Getting runners' times to check for split pace faster than 4:30
runnerTimes[0] = recent.getTimes(runners[0]);
runnerTimes[1] = recent.getTimes(runners[1]);
runnerTimes[2] = recent.getTimes(runners[2]);
for (int i = 0; i < 3; i++)
{
splits[i][0] = recent.getSplits(runnerTimes[i], 0);
splits[i][1] = recent.getSplits(runnerTimes[i], 1);
//System.out.println("Racer " + runners[i] + " Split1: " + splits[i][0] + " Splits 2: " + splits[i][1]);
if (splits[i][0] <= 270 || splits[i][1] <=270){
System.out.println("Racer " + runners[i] + " has a split time faster " +
" than 4:30 a mile, runner possibly cheated.");
}
}
System.out.println("-- Cheaters list above, unless data appeared legitimate --");
} catch (FileNotFoundException e) {
System.out.println("Cannot find file " + fileName);
}
}
}
/*
Kurt Kaiser
CTIM 168
07.28.2018
Homework: C10PP10
RFID Race Data Text File Log
*/
import java.io.*;
import java.util.Scanner;
public class RaceDataLog {
private String[][] data = new String[9][];
public String[][] getData() {
return data;
}
// Constructor
public RaceDataLog(String fileName) throws FileNotFoundException {
Scanner inputStream = new Scanner(new File(fileName));
String startTime = inputStream.nextLine();
int count = 0;
String breakUp = "[, ]";
// Read the rest of the file line by line
while (inputStream.hasNextLine()) {
// Use split to create two dimensional array
data[count] = inputStream.nextLine().split(breakUp);
count++;
}
inputStream.close();
}
// Returns run time in seconds of requested number
public int[] getTimes(String input) {
int[] runnerTimes = new int[3];
String[][] data = getData();
int count = 0;
int intTime;
int place = 1;
for (int i = 0; i < data.length; i++) {
if (input.contentEquals(data[i][1])) {
intTime = 3600 * Integer.parseInt(data[i][2]);
intTime += 60 * Integer.parseInt(data[i][3]);
intTime += Integer.parseInt(data[i][4]);
runnerTimes[count] = intTime;
count++;
}
}
return runnerTimes;
}
// Calculate runners pace between markers, output to console
public int getSplits(int[] runnerTimes, int startSplit) {
int dist = 1;
if (startSplit == 0) {
dist = 7;
} else if (startSplit == 1) {
dist = 6;
} else {
return 0;
}
int split = (runnerTimes[startSplit + 1] - runnerTimes[startSplit]) / dist;
return split;
}
public String getFormattedTime(int total){
int[] timeArray = new int[3];
String strTime;
String result = "";
timeArray[0] = total / 3600;
timeArray[1] = (total - (timeArray[0] * 3600)) / 60;
timeArray[2] = total - timeArray[0] * 3600 - timeArray[1] * 60;
for (int i = 0; i < 3; i++) {
strTime = String.valueOf(timeArray[i]);
if (strTime.length() == 1) strTime = "0" + strTime;
result += strTime;
if (i < 2) result += ":";
}
return result;
}
// Checks if a runner skipped a sensor
public String[] skippedSensor(String fileName) throws IOException {
String[] racers = {"na", "na", "na"};
int[] sensorCount = {0, 0, 0};
String line;
Scanner inputStream = new Scanner(new File(fileName));
// First line race start time, skip over it
inputStream.nextLine();
// Read the rest of the file line by line
while (inputStream.hasNextLine()) {
// Use split to create two dimensional array
line = inputStream.nextLine();
line = line.substring(2, 5);
for (int i = 0; i < 3; i++) {
if (racers[i].equals(line)) {
sensorCount[i]++;
break;
} else if(racers[i].equals("na")){
racers[i] = line;
sensorCount[i]++;
break;
}
}
}
// Check for cheaters and print numbers to console
for (int j = 0; j < 3; j++){
//System.out.println("Runner: " + racers[j] + " Sensors: " + sensorCount[j]);
if (sensorCount[j] < 3 && sensorCount[j] > 0) {
System.out.println("Runner " + racers[j] +
" missed a sensor, runner possibly cheated.");
System.out.println("Data incomplete. Correct data and run again to check for cheating during splits.");
racers[0] = "cheated";
}
}
return racers;
}
}
08 00 00
0,100,08 00 00
0,132,08 00 03
0,182,08 00 15
1,100,08 50 46
1,132,08 51 18
2,132,09 34 16
2,100,09 35 10
2,182,09 45 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment