Created
October 6, 2012 12:39
-
-
Save xaviablaza-zz/3844825 to your computer and use it in GitHub Desktop.
A simple Tic-Tac-Toe game.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
/** | |
* | |
* @author MeneXia (Xavi Ablaza) | |
* | |
*/ | |
public class TicTacToe { | |
static Scanner in; | |
static String[] board; | |
static String turn; | |
public static void main(String[] args) { | |
in = new Scanner(System.in); | |
board = new String[9]; | |
turn = "X"; | |
String winner = null; | |
populateEmptyBoard(); | |
System.out.println("Welcome to 2 Player Tic Tac Toe."); | |
System.out.println("--------------------------------"); | |
printBoard(); | |
System.out.println("X's will play first. Enter a slot number to place X in:"); | |
while (winner == null) { | |
int numInput; | |
try { | |
numInput = in.nextInt(); | |
if (!(numInput > 0 && numInput <= 9)) { | |
System.out.println("Invalid input; re-enter slot number:"); | |
continue; | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("Invalid input; re-enter slot number:"); | |
continue; | |
} | |
if (board[numInput-1].equals(String.valueOf(numInput))) { | |
board[numInput-1] = turn; | |
if (turn.equals("X")) { | |
turn = "O"; | |
} else { | |
turn = "X"; | |
} | |
printBoard(); | |
winner = checkWinner(); | |
} else { | |
System.out.println("Slot already taken; re-enter slot number:"); | |
continue; | |
} | |
} | |
if (winner.equalsIgnoreCase("draw")) { | |
System.out.println("It's a draw! Thanks for playing."); | |
} else { | |
System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing."); | |
} | |
} | |
static String checkWinner() { | |
for (int a = 0; a < 8; a++) { | |
String line = null; | |
switch (a) { | |
case 0: | |
line = board[0] + board[1] + board[2]; | |
break; | |
case 1: | |
line = board[3] + board[4] + board[5]; | |
break; | |
case 2: | |
line = board[6] + board[7] + board[8]; | |
break; | |
case 3: | |
line = board[0] + board[3] + board[6]; | |
break; | |
case 4: | |
line = board[1] + board[4] + board[7]; | |
break; | |
case 5: | |
line = board[2] + board[5] + board[8]; | |
break; | |
case 6: | |
line = board[0] + board[4] + board[8]; | |
break; | |
case 7: | |
line = board[2] + board[4] + board[6]; | |
break; | |
} | |
if (line.equals("XXX")) { | |
return "X"; | |
} else if (line.equals("OOO")) { | |
return "O"; | |
} | |
} | |
for (int a = 0; a < 9; a++) { | |
if (Arrays.asList(board).contains(String.valueOf(a+1))) { | |
break; | |
} | |
else if (a == 8) return "draw"; | |
} | |
System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:"); | |
return null; | |
} | |
static void printBoard() { | |
System.out.println("/---|---|---\\"); | |
System.out.println("| " + board[0] + " | " + board[1] + " | " + board[2] + " |"); | |
System.out.println("|-----------|"); | |
System.out.println("| " + board[3] + " | " + board[4] + " | " + board[5] + " |"); | |
System.out.println("|-----------|"); | |
System.out.println("| " + board[6] + " | " + board[7] + " | " + board[8] + " |"); | |
System.out.println("/---|---|---\\"); | |
} | |
static void populateEmptyBoard() { | |
for (int a = 0; a < 9; a++) { | |
board[a] = String.valueOf(a+1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry for being quite late to the party but ill still break it down for you, somethings i'll explain but I wont use the correct terms, whoever is reading this and knows any of the correct terms, feel free to correct them. If you have any specific questions ask them.
if (board[numInput-1].equals(String.valueOf(numInput))) {
Lets start out with this line, this line of code is the beginning of an if statement, an if statement allows you to put something inside the following parenthesis and if everything inside of them is true then whatever is in the curly brackets are run. Inside the parenthesis can be simplified when explaining, board[numInput-1] == String.valueOf(numInput), the beginning part is calling an array, you call an array by naming the variable and then putting brackets, inside the brackets is the index of which point in the array you want to call. What they're calling is numInput-1, numInput is a variable and there is a -1 because arrays start at 0 and work their way up, i would believe numInput would start at 1 instead of 0 to make debugging easier for you to read. The following function .equals() just checks if the argument you put into the function is equal to the variable it is being used to. String.valueOf() is another function, I haven't used this function so I don't know exactly what it does, my best guess is it turns a float/int into a string.
board[numInput-1] = turn;
This line is simply making whatever point on the array we are at, and making that the character of whoever's turn it is.
if (turn.equals("X")) {
turn = "O";
} else {
turn = "X";
}
This if statement is just there to change who's turn it is. If it was X's turn its now O's turn and vice versa.
printBoard();
This just calls the function printBoard() that was made within the script.
winner = checkWinner();
This sets the winner variable to the output of the checkWinner() function.
System.out.println("Slot already taken; re-enter slot number:");
This is a function within java that writes to the console, anything that is inside the parenthesis will be written to the console, this includes floats, integers, characters, and strings.
continue;
I don't know what this line of code is supposed to do either, if someone could help me out with this one that would be much appreciated.