Created
April 13, 2018 04:26
-
-
Save guillermo-menjivar/d601efd80b122def90cf34e36b9631c3 to your computer and use it in GitHub Desktop.
somejava.java
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
import java.util.*; | |
public class TicTacToe { | |
public static void main(String[] args) { | |
int[][]board= new int [3][3]; | |
int turn= 1; | |
while(full(board)== false && winner(board)==0) | |
{ | |
printBoard(board); | |
System.out.println("Human, play!"); | |
humanPlay(board,turn); | |
if(turn ==1) | |
{ | |
turn=2; | |
} | |
else | |
{ | |
turn=1; | |
} | |
int winner = winner(board); | |
//printBoard(board); | |
if(winner==1) | |
{ | |
System.out.println("Congrats X! You won!"); | |
} | |
else | |
if(winner==2) | |
{ | |
System.out.println("Congrats O! You won!"); | |
} | |
//else | |
//{ | |
// System.out.println("It's a draw!"); | |
//} | |
} | |
if (winner(board) == 0) | |
{ | |
System.out.println("It's a draw!"); | |
} | |
} | |
public static boolean full(int[][] board) { | |
for(int row = 0; row < board.length; row++) | |
{ | |
for(int col = 0;col < board[0].length; col++) | |
{ | |
if(board[row][col]==0) | |
return false; | |
} | |
} | |
return true; | |
} | |
public static void printBoard(int[][] board) | |
{ | |
// String hbar = "|"; | |
// String vbar = "----"; | |
// | |
// for (int row=0; row < board.length; row++) | |
// { | |
// for (int col=0; col < board[0].length; col++) | |
// { | |
// if (col > 0) | |
// { | |
// System.out.print(hbar); | |
// } | |
// if (board[row][col] == 1) | |
// { | |
// System.out.print("X"); | |
// } | |
// else if (board[row][col] == 2) | |
// { | |
// System.out.print("O"); | |
// } | |
// else | |
// { | |
// System.out.print(" "); | |
// } | |
// if (col == 2) | |
// { | |
// System.out.println(""); | |
// } | |
// if (row<2) | |
// { | |
// System.out.print(hbar); | |
// } | |
// } | |
// } | |
for (int row=0; row < board.length; row++) | |
{ | |
//if (row == 2) | |
//{ | |
if (board [row][0]== 0) | |
{ | |
System.out.print(" "); | |
} | |
else | |
if (board [row][0]== 1) | |
{ | |
System.out.print(" X "); | |
} | |
else | |
if (board [row][0]== 2) | |
{ | |
System.out.print(" O "); | |
} | |
if (board [row][1] ==0) | |
{ | |
System.out.print("| |"); | |
} | |
else | |
if (board [row][1] ==1) | |
{ | |
System.out.print("| X |"); | |
} | |
else | |
if (board [row][1] ==2) | |
{ | |
System.out.print("| O |"); | |
} | |
if (board [row][2] == 0) | |
{ | |
System.out.print(" "); | |
} | |
else | |
if (board [row][2] == 1) | |
{ | |
System.out.print(" X "); | |
} | |
else | |
if (board [row][2] == 2) | |
{ | |
System.out.print(" O "); | |
} | |
//} | |
System.out.println(""); | |
System.out.println("-----------"); | |
//{ | |
// if (board [row][0] == 0) | |
// { | |
// System.out.print(" |"); | |
// System.out.println("----"); | |
// } | |
// else | |
// if (board [row][0] == 1) | |
// { | |
// System.out.println(" X |"); | |
// System.out.println("----"); | |
// } | |
// else | |
// if (board [row][0] == 2) | |
// { | |
// System.out.println(" O |"); | |
// System.out.println("----"); | |
// } | |
// if (board [row][1] == 0) | |
// { | |
// System.out.println("| |"); | |
// System.out.println("----"); | |
// } | |
// else | |
// if (board [row][1] == 1) | |
// { | |
// System.out.println("| X |"); | |
// System.out.println("----"); | |
// } | |
// else | |
// if (board [row][1] == 2) | |
// { | |
// System.out.println("| O |"); | |
// System.out.println("----"); | |
// } | |
// if (board [row][2] == 0) | |
// { | |
// System.out.println("| "); | |
// System.out.println("----"); | |
// } | |
// else | |
// if (board [row][2] == 1) | |
// { | |
// System.out.println("| X "); | |
// System.out.println("----"); | |
// } | |
// else | |
// if (board [row][2] == 2) | |
// { | |
// System.out.println("| O "); | |
// System.out.println("----"); | |
// } | |
//} | |
} | |
} | |
public static int winner(int[][] board) { | |
int[]players = new int[2]; | |
int winner = 0; // if winner remains 0 then is a draw | |
players[0] = 1; // add player one to the array of player to check for winners | |
players[1] = 2; // add player two to the array of players to check for winners | |
// check for the winner or draw | |
for (int p=0; p < players.length; p++) | |
{ | |
int player = players[p]; | |
if (board[0][0] == player && board[0][1] == player && board[0][2] == player){ | |
winner = player; | |
} | |
if (board[1][0] == player && board[1][1] == player && board[1][2] == player){ | |
winner = player; | |
} | |
if (board[2][0] == player && board[2][1] == player && board[2][2] == player){ | |
winner = player; | |
} | |
if (board[0][0] == player && board[1][0] == player && board[2][0] == player){ | |
winner = player; | |
} | |
if (board[0][1] == player && board[1][1] == player && board[2][1] == player){ | |
winner = player; | |
} | |
if (board[0][2] == player && board[1][2] == player && board[2][2] == player){ | |
winner = player; | |
} | |
//diagonal wins | |
if (board[0][0] == player && board[1][1] == player && board[2][2] == player){ | |
winner = player; | |
} | |
if (board[2][0] == player && board[1][1] == player && board[0][2] == player){ | |
winner = player; | |
} | |
} | |
return winner; | |
} | |
//for(int row = 0; row < board.length; row++) | |
//{ | |
// for(int col = 0;col < board[0].length; col++) | |
// { | |
// if(board [row][col] == 1) | |
// { | |
// row++; | |
// if(board [row][col]== 1) | |
// row++; | |
// { | |
// if(board [row][col]== 1) | |
// System.out.println("X is the winner! "); | |
// return 1; | |
// } | |
// } | |
// else | |
// if(board [row][col] == 1) | |
// { | |
// col++; | |
// if(board [row][col]== 1) | |
// col++; | |
// { | |
// if(board [row][col]== 1) | |
// System.out.println("X is the winner! "); | |
// return 1; | |
// } | |
// } | |
// else | |
// if(board [row][col] == 2) | |
// { | |
// row++; | |
// if(board [row][col]== 2) | |
// row++; | |
// { | |
// if(board [row][col]== 2) | |
// System.out.println("O is the winner! "); | |
// return 2; | |
// } | |
// } | |
// else | |
// if(board [row][col] == 2) | |
// { | |
// col++; | |
// if(board [row][col]== 2) | |
// col++; | |
// { | |
// if(board [row][col]== 2) | |
// System.out.println("O is the winner! "); | |
// return 2; | |
// } | |
// } | |
// else | |
// if(board [1][1]== 1) | |
// { | |
// if(board [0][0]==1) | |
// { | |
// if(board [2][2]==1) | |
// System.out.println("X is the winner!!"); | |
// return 1; | |
// } | |
// else | |
// if(board [0][2]== 1) | |
// { | |
// if(board [2][0]== 1) | |
// System.out.println("X is the winner!!"); | |
// return 1; | |
// } | |
// } | |
// else | |
// if(board [1][1]== 2) | |
// { | |
// if(board [0][0]==2) | |
// { | |
// if(board [2][2]==2) | |
// System.out.println("O is the winner!!"); | |
// return 2; | |
// } | |
// else | |
// if(board [0][2]== 2) | |
// { | |
// if(board [2][0]== 2) | |
// System.out.println("O is the winner!!"); | |
// return 2; | |
// } | |
// } | |
// } | |
//} | |
//return 0; | |
//} | |
public static void humanPlay(int[][] board, int playNum) { | |
Scanner sc = new Scanner(System.in); | |
int row = -1; | |
int col = -1; | |
while(row >= board.length || row < 0 || col >= board[0].length || col < 0 || board[row][col] != 0) | |
//while(row < board.length && col < board[0].length && board[row][col]==0) | |
{ | |
if(playNum == 1) | |
System.out.print("Player X, select a row: "); | |
else | |
System.out.print("Player O, select a row: "); | |
row = sc.nextInt(); | |
if(playNum == 1) | |
System.out.print("Player X, select a column: "); | |
else | |
System.out.print("Player O, select a column: "); | |
col = sc.nextInt(); | |
} | |
System.out.println("returninig"); | |
board[row][col] = playNum; | |
} | |
//bonus | |
public static void randomPlay(int[][] board, int playNum) { | |
} | |
//bonus | |
public static void intelligentPlay(int[][] board, int playNum) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment