mirror of
https://github.com/Xevion/contest.git
synced 2025-12-15 10:11:27 -06:00
feb 2015 problem 11 and 12
This commit is contained in:
20
uil/aplus-february-2015/12/TicTacToe.MD
Normal file
20
uil/aplus-february-2015/12/TicTacToe.MD
Normal file
@@ -0,0 +1,20 @@
|
||||
# TicTacToe
|
||||
|
||||
Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-TicTacToe)!**
|
||||
|
||||
A overall very fun problem, one of my absolute favorite in this set. Inputs were easy to understand, and I believe my implementation was short, sweet, and easy to understand (I feel clever, give me kudos)!
|
||||
|
||||
Understanding the flow of decision is important here, as otherwise you'll get weird outputs, even if your code evaluated 'as it should' (your implementation worked as it should, even if your implementation did not fit the problem's specifications).
|
||||
* First, you should check Verticals/Horizontals/Diagonals for a actual *solutions* (three identical marks in a row). If one is found, that's the answer.
|
||||
* Second, you should check that all places are filled in. If not, this is counted as a "Incomplete Board", and you should return in turn.
|
||||
* Third, if the above two don't match up, all spots are filled in, yet no one has won - it's a tie, return as such.
|
||||
|
||||
My implementation essentially came down to implementing methods that returned 3 item long arrays of rows, columns, and diagonal spaces. These were given a input of the common X or Y coordinate associated (diagonal used a 'rising' and 'falling' argument instead [True/False]).
|
||||
|
||||
Then, in the actual main method that 'solved' it, I iterated over the data, calling these methods and checking them against a 'equals' method, which simply checked that all 3 elements were equal to eachother using `String.equals`.
|
||||
|
||||
Implementation has already been explained above, so as a last word of advice when solving - make sure that your input data is correctly typed in! Since I manually did it based on what my paper said, I wasn't as careful as I could have been, and simply assumed that the data was correct (as it should be in real competition, as you receive a file, not type it yourself).
|
||||
|
||||
I failed to capitalize an `X` correctly and the `String.equals` method failed to pick up this. One could argue using `String.toUpperCase` would be a valid way of ensuring that the input data is case-insensitive.
|
||||
|
||||
Overall, an interesting problem that challenged me and my understanding of matrixes when implementing `vertical` and `horizontal` data aggregation methods.
|
||||
89
uil/aplus-february-2015/12/TicTacToe.java
Normal file
89
uil/aplus-february-2015/12/TicTacToe.java
Normal file
@@ -0,0 +1,89 @@
|
||||
import static java.lang.System.out;
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
class Board {
|
||||
private String[][] board;
|
||||
|
||||
Board(String input) {
|
||||
this.board = new String[3][3];
|
||||
for(int i = 0; i < 9; i++)
|
||||
this.board[i / 3][i % 3] = input.substring(i, i + 1);
|
||||
}
|
||||
|
||||
// [[A, B, C], [D, E, F], [G, H, I]]
|
||||
// A B C | 0,0 1,0 2,0
|
||||
// D E F | 0,1 1,1 2,1
|
||||
// G H I | 0,2 1,2 2,2
|
||||
|
||||
public String[] vertical(int x) {
|
||||
return this.board[x];
|
||||
}
|
||||
public String[] horizontal(int y) {
|
||||
String[] vert = new String[3];
|
||||
for(int x = 0; x < 3; x++)
|
||||
vert[x] = this.board[x][y];
|
||||
return vert;
|
||||
}
|
||||
|
||||
public String[] diagonal(boolean rising) {
|
||||
String[] diag = new String[3];
|
||||
if(rising) {
|
||||
for(int i = 0; i < 3; i++)
|
||||
diag[i] = this.board[2 - i][2 - i];
|
||||
} else {
|
||||
for(int i = 0; i < 3; i++)
|
||||
diag[i] = this.board[i][i];
|
||||
}
|
||||
return diag;
|
||||
}
|
||||
|
||||
public boolean equal(String[] line) {
|
||||
return line[0].equals(line[1]) && line[1].equals(line[2]) && !line[0].equals("*");
|
||||
}
|
||||
|
||||
public String solve() {
|
||||
// Verticals
|
||||
for(int x = 0; x < 3; x++)
|
||||
if(this.equal(this.vertical(x)))
|
||||
return this.vertical(x)[0];
|
||||
// Horizontals
|
||||
for(int y = 0; y < 3; y++)
|
||||
if(this.equal(this.horizontal(y)))
|
||||
return this.horizontal(y)[0];
|
||||
// Diagonals
|
||||
if(this.equal(this.diagonal(false)))
|
||||
return this.diagonal(false)[0];
|
||||
if(this.equal(this.diagonal(true)))
|
||||
return this.diagonal(true)[0];
|
||||
|
||||
// Check for incompletion
|
||||
for(int x = 0; x < 3; x++) {
|
||||
for(int y = 0; y < 3; y++) {
|
||||
if(this.board[x][y].equals("*"))
|
||||
return "INC";
|
||||
}
|
||||
}
|
||||
|
||||
// Tie
|
||||
return "TIE";
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("Board(%s)", Arrays.deepToString(this.board));
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Scanner s = new Scanner(new File("tictactoe.dat"));
|
||||
s.nextLine();
|
||||
while(s.hasNextLine()) {
|
||||
String input = s.nextLine();
|
||||
Board board = new Board(input);
|
||||
out.println(board.solve());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user