Last active
August 29, 2015 14:19
-
-
Save mariack/0ab6ab0f2d742b2086fc to your computer and use it in GitHub Desktop.
This file contains 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
package assignments; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
public class TicTacToe | |
{ | |
Scanner keyboard; | |
char input; | |
int i, j, coordinate1, coordinate2; | |
boolean noWinner; | |
char[][] board = new char[3][3]; | |
public void setBoard() | |
{ | |
System.out.println("Let's play Tic Tac Toe.\n"); | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
board[i][j] = '*'; | |
System.out.print(board[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
} | |
public void showCoordinates() | |
{ | |
System.out.println("Here are the coordinates(1st, 2nd):\n"); | |
String[][] coordinate = new String[3][3]; | |
for(i = 0; i < coordinate.length; i++) | |
{ | |
for(j = 0; j < coordinate.length; j++) | |
{ | |
coordinate[0][0] = "(0,0)"; | |
coordinate[0][1] = "(0,1)"; | |
coordinate[0][2] = "(0,2)"; | |
coordinate[1][0] = "(1,0)"; | |
coordinate[1][1] = "(1,1)"; | |
coordinate[1][2] = "(1,2)"; | |
coordinate[2][0] = "(2,0)"; | |
coordinate[2][1] = "(2,1)"; | |
coordinate[2][2] = "(2,2)"; | |
System.out.print(coordinate[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
} | |
public void userInput() | |
{ | |
keyboard = new Scanner(System.in); | |
System.out.print("Enter X or O: "); | |
input = keyboard.next().charAt(0); | |
input = Character.toUpperCase(input); | |
while(input != 'x' && input != 'o' && input != 'X' && input != 'O') | |
{ | |
System.out.println("Invalid input. Enter X or O. Try again"); | |
System.out.print("Enter X or O: "); | |
input = keyboard.next().charAt(0); | |
} | |
System.out.print("Enter 1st coordinate: "); | |
coordinate1 = keyboard.nextInt(); | |
while(coordinate1 != 1 && coordinate1 != 2 && coordinate1 != 0) | |
{ | |
debugCoordinate1(); | |
} | |
System.out.print("Enter 2nd coordinate: "); | |
coordinate2 = keyboard.nextInt(); | |
while(coordinate2 != 1 && coordinate2 != 2 && coordinate2 != 0) | |
{ | |
debugCoordinate2(); | |
} | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
board[coordinate1][coordinate2] = input; | |
System.out.print(board[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
noWinner = true; | |
while(noWinner) | |
{ | |
switchPlayer(); | |
System.out.printf("Enter 1st coordinate(Player '%s'): ", input); | |
coordinate1 = keyboard.nextInt(); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): ", input); | |
coordinate2 = keyboard.nextInt(); | |
while(coordinate1 != 1 && coordinate1 != 2 && coordinate1 != 0) | |
{ | |
debugCoordinate1(); | |
} | |
while(coordinate2 != 1 && coordinate2 != 2 && coordinate2 != 0) | |
{ | |
debugCoordinate1(); | |
} | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
board[coordinate1][coordinate2] = input; | |
System.out.print(board[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
checkRow(); | |
checkCol(); | |
checkDiagonal(); | |
checkBoard(); | |
} | |
} | |
public void switchPlayer() | |
{ | |
if(input == 'x' || input == 'X') | |
{ | |
input = 'O'; | |
} | |
else | |
{ | |
input = 'X'; | |
} | |
} | |
private boolean areEqual(char x, char y, char z) | |
{ | |
return((x != '*') && (x == y) && (z == x)); | |
} | |
public boolean checkCol() | |
{ | |
for(i = 0; i < board.length; i++) | |
{ | |
if(areEqual(board[i][0], board[i][1], board[i][2]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
} | |
return noWinner; | |
} | |
public boolean checkRow() | |
{ | |
for(i = 0; i < board.length; i++) | |
{ | |
if(areEqual(board[0][i], board[1][i], board[2][i]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
} | |
return noWinner; | |
} | |
public boolean checkDiagonal() | |
{ | |
if(areEqual(board[0][0], board[1][1], board[2][2]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
else if(areEqual(board[0][2], board[1][1], board[2][0]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
return noWinner; | |
} | |
public boolean checkBoard() | |
{ | |
boolean full = true; | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
if(board[i][j] == '*') | |
{ | |
full = false; | |
} | |
} | |
} | |
return full; | |
} | |
public int debugCoordinate1() | |
{ | |
System.out.println("Invalid coordinate. Refer to the list of coordinates above."); | |
System.out.printf("Enter 1st coordinate(Player '%s'): ", input); | |
coordinate1 = keyboard.nextInt(); | |
return coordinate1; | |
} | |
public int debugCoordinate2() | |
{ | |
System.out.println("Invalid coordinate. Refer to the list of coordinates above."); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): ", input); | |
coordinate2 = keyboard.nextInt(); | |
return coordinate2; | |
} | |
} |
This file contains 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
package assignments; | |
public class TicTacToeDemo | |
{ | |
public static void main(String[] args) | |
{ | |
TicTacToe play; | |
play = new TicTacToe(); | |
play.setBoard(); | |
play.showCoordinates(); | |
play.userInput(); | |
} | |
} |
This file contains 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
package assignments; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
import java.util.Random; | |
public class TicTacToe | |
{ | |
Scanner keyboard; | |
char input; | |
int i, j, coordinate1, coordinate2; | |
boolean noWinner; | |
char choice; | |
int randomInt; | |
char[][] board = new char[3][3]; | |
public void setBoard() | |
{ | |
System.out.println("Let's play Tic Tac Toe.\n"); | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
board[i][j] = '*'; | |
System.out.print(board[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
} | |
public void showCoordinates() | |
{ | |
System.out.println("Here are the coordinates(1st, 2nd):\n"); | |
String[][] coordinate = new String[3][3]; | |
for(i = 0; i < coordinate.length; i++) | |
{ | |
for(j = 0; j < coordinate.length; j++) | |
{ | |
coordinate[0][0] = "(0,0)"; | |
coordinate[0][1] = "(0,1)"; | |
coordinate[0][2] = "(0,2)"; | |
coordinate[1][0] = "(1,0)"; | |
coordinate[1][1] = "(1,1)"; | |
coordinate[1][2] = "(1,2)"; | |
coordinate[2][0] = "(2,0)"; | |
coordinate[2][1] = "(2,1)"; | |
coordinate[2][2] = "(2,2)"; | |
System.out.print(coordinate[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
} | |
public void userInput() | |
{ | |
choosePlayer(); | |
if (choice == 'C' || choice == 'c') | |
{ | |
System.out.println("User, you are 'X's'. Computer, you are 'O's'"); | |
input = 'X'; | |
} | |
else | |
{ | |
System.out.print("Enter X or O: "); | |
keyboard = new Scanner(System.in); | |
input = keyboard.next().charAt(0); | |
input = Character.toUpperCase(input); | |
} | |
while(input != 'x' && input != 'o' && input != 'X' && input != 'O') | |
{ | |
System.out.println("Invalid input. Enter X or O. Try again"); | |
System.out.print("Enter X or O: "); | |
input = keyboard.next().charAt(0); | |
} | |
System.out.print("Enter 1st coordinate: "); | |
coordinate1 = keyboard.nextInt(); | |
while(coordinate1 != 1 && coordinate1 != 2 && coordinate1 != 0) | |
{ | |
debugCoordinate1(); | |
} | |
System.out.print("Enter 2nd coordinate: "); | |
coordinate2 = keyboard.nextInt(); | |
while(coordinate2 != 1 && coordinate2 != 2 && coordinate2 != 0) | |
{ | |
debugCoordinate2(); | |
} | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
board[coordinate1][coordinate2] = input; | |
System.out.print(board[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
noWinner = true; | |
while(noWinner) | |
{ | |
switchPlayer(); | |
Random randomGenerator = new Random(); | |
if(choice == 'C' || choice == 'c') | |
{ | |
if(input == 'x' || input == 'X') | |
{ | |
System.out.printf("Enter 1st coordinate(Player '%s'): ", input); | |
coordinate1 = keyboard.nextInt(); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): ", input); | |
coordinate2 = keyboard.nextInt(); | |
} | |
else if (input == 'O' || input == 'o') | |
{ | |
System.out.printf("Enter 1st coordinate(Player '%s'): \n", input); | |
coordinate1 = randomGenerator.nextInt(3); | |
System.out.println("For coordinate 1, computer entered: " + coordinate1); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): \n", input); | |
coordinate2 = randomGenerator.nextInt(3); | |
System.out.println("For coordinate 2, computer entered: " + coordinate2); | |
} | |
} | |
else | |
{ | |
System.out.printf("Enter 1st coordinate(Player '%s'): ", input); | |
coordinate1 = keyboard.nextInt(); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): ", input); | |
coordinate2 = keyboard.nextInt(); | |
} | |
while(coordinate1 != 1 && coordinate1 != 2 && coordinate1 != 0) | |
{ | |
debugCoordinate1(); | |
} | |
while(coordinate2 != 1 && coordinate2 != 2 && coordinate2 != 0) | |
{ | |
debugCoordinate2(); | |
} | |
while(board[coordinate1][coordinate2] != '*') | |
{ | |
System.out.println("\n"); | |
System.out.println("Coordinate taken. Try another."); | |
if(input == 'x' || input == 'X') | |
{ | |
System.out.printf("Enter 1st coordinate(Player '%s'): ", input); | |
coordinate1 = keyboard.nextInt(); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): ", input); | |
coordinate2 = keyboard.nextInt(); | |
} | |
else if (input == 'O' || input == 'o') | |
{ | |
System.out.printf("Enter 1st coordinate(Player '%s'): \n", input); | |
coordinate1 = randomGenerator.nextInt(3); | |
System.out.println("For coordinate 1, computer entered: " + coordinate1); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): \n", input); | |
coordinate2 = randomGenerator.nextInt(3); | |
System.out.println("For coordinate 2, computer entered: " + coordinate2); | |
} | |
} | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
board[coordinate1][coordinate2] = input; | |
System.out.print(board[i][j] + " "); | |
} | |
System.out.println("\n"); | |
} | |
checkRow(); | |
checkCol(); | |
checkDiagonal(); | |
checkBoard(); | |
} | |
} | |
public void switchPlayer() | |
{ | |
if(input == 'x' || input == 'X') | |
{ | |
input = 'O'; | |
} | |
else | |
{ | |
input = 'X'; | |
} | |
} | |
private boolean areEqual(char x, char y, char z) | |
{ | |
return((x != '*') && (x == y) && (z == x)); | |
} | |
public boolean checkCol() | |
{ | |
for(i = 0; i < board.length; i++) | |
{ | |
if(areEqual(board[i][0], board[i][1], board[i][2]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
} | |
return noWinner; | |
} | |
public boolean checkRow() | |
{ | |
for(i = 0; i < board.length; i++) | |
{ | |
if(areEqual(board[0][i], board[1][i], board[2][i]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
} | |
return noWinner; | |
} | |
public boolean checkDiagonal() | |
{ | |
if(areEqual(board[0][0], board[1][1], board[2][2]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
else if(areEqual(board[0][2], board[1][1], board[2][0]) == true) | |
{ | |
System.out.printf("Player '%s' wins!\n", input); | |
noWinner = false; | |
} | |
return noWinner; | |
} | |
public boolean checkBoard() | |
{ | |
boolean full = true; | |
for(i = 0; i < board.length; i++) | |
{ | |
for(j = 0; j < board.length; j++) | |
{ | |
if(board[i][j] == '*') | |
{ | |
full = false; | |
} | |
} | |
} | |
return full; | |
} | |
public int debugCoordinate1() | |
{ | |
System.out.println("Invalid coordinate. Refer to the list of coordinates above."); | |
System.out.printf("Enter 1st coordinate(Player '%s'): ", input); | |
coordinate1 = keyboard.nextInt(); | |
return coordinate1; | |
} | |
public int debugCoordinate2() | |
{ | |
System.out.println("Invalid coordinate. Refer to the list of coordinates above."); | |
System.out.printf("Enter 2nd coordinate(Player '%s'): ", input); | |
coordinate2 = keyboard.nextInt(); | |
return coordinate2; | |
} | |
public char choosePlayer() | |
{ | |
System.out.println("Do you want to play the computer or another person?"); | |
System.out.print("Enter C for computer or T for two player: "); | |
keyboard = new Scanner(System.in); | |
choice = keyboard.next().charAt(0); | |
return choice; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment