Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Tic Tac Toe Program Sample output from your program should look like th

ID: 3531019 • Letter: C

Question

Create a Tic Tac Toe Program Sample output from your program should look like the following: C:work>java TicTacToe ******************************** ---a------b------c--- | | | | ---d------e------f--- | | | | ---g------h------i--- | | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i] e ******************************** ---a------b------c--- | | | | ---d------e------f--- | | O | | ---g------h------i--- | | | | --------------------- Entering input for: player2: X enter in cell [a, b, ... i] a ******************************** ---a------b------c--- | X | | | ---d------e------f--- | | O | | ---g------h------i--- | | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i] i ******************************** ---a------b------c--- | X | | | ---d------e------f--- | | O | | ---g------h------i--- | | | O | --------------------- Entering input for: player2: X enter in cell [a, b, ... i] b ******************************** ---a------b------c--- | X | X | | ---d------e------f--- | | O | | ---g------h------i--- | | | O | --------------------- Entering input for: player1: O enter in cell [a, b, ... i] c ******************************** ---a------b------c--- | X | X | O | ---d------e------f--- | | O | | ---g------h------i--- | | | O | --------------------- Entering input for: player2: X enter in cell [a, b, ... i] f ******************************** ---a------b------c--- | X | X | O | ---d------e------f--- | | O | X | ---g------h------i--- | | | O | --------------------- Entering input for: player1: O enter in cell [a, b, ... i] g We have a winner: player1: O ******************************** ---a------b------c--- | X | X | O | ---d------e------f--- | | O | X | ---g------h------i--- | O | | O | --------------------- Do you want to play Tic-Tac-Toe (y/n)? ************************************************************ A starting point for your program is provided (if you feel overly constrained by this and have a better idea on structuring this game you can create your own structure -- minimize the code you put in main): import java.util.*; class TicTacToe { char ttt[][] = new char[3][3]; static final char player1 = 'O'; static final char player2 = 'X'; Scanner scan =new Scanner(System.in); String playerID(char player) { // Prints the identity of the player // For example: // player2: X if (player == player1) return "player1: "+player; else return "player2: "+ player; } void getPlayerInput(char player) { // ******* Details to fill in ************ // Prompt the user for a cell input. Make sure its a legal // cell designator. Also make sure the cell doesn't already // have a player in it. Prompt the user again in the case // of any problem. Once a valid spot has been found, // you will issue a statement like: ttt[row][col]=player; } boolean gameIsDraw() { // ******* Details to fill in ************ // If all ttt entries have been taken return true // otherwise return false } boolean winner(char player) { // ******* Details to fill in ************ // Check to see if the parameter player has won // Winning means that player shows up in a line of // three. The line can be a column, row or a diagonal // Return true if player is a winner, otherwise return false. } void displayBoard() { // ******* Details to fill in ************ // Display the current status of the tic-tac-toe board } void newgame() { char currPlayer = player1; for(int i=0; i<3; i++) for(int j=0; j<3; j++) ttt[i][j] =' '; boolean continueFlag = true; while (continueFlag) { displayBoard(); if (gameIsDraw()) { System.out.println("Game Ends in Draw"); continueFlag = false; } else { getPlayerInput(currPlayer); if (winner(currPlayer)) { System.out.println("We have a winner: " + playerID(currPlayer)); displayBoard(); continueFlag = false; } else { if (currPlayer == player1) currPlayer = player2; else currPlayer = player1; } } } } public static void main(String[] args) { TicTacToe game = new TicTacToe(); String str; do { game.newgame(); System.out.println("Do you want to play Tic-Tac-Toe (y/n)?"); str= game.scan.next(); } while ("y".equals(str)); System.out.println("Bye"); } } By the way, I'm going to give a version of displayBoard(). This routine is a bit of a pain and I'm not sure how much learning happens by going through the pain. Also, I'm not real happy with my version, but it at least works. So... if you want to use my version, here it is: void displayBoard() { System.out.println("********************************"); System.out.println(" ---a------b------c---"); for (int i=0; i<3; i++) { for (int j=0; j< 3; j++) { if (j == 0) System.out.print(" | "); System.out.print(ttt[i][j]); if (j < 2) System.out.print( " | "); if (j==2) System.out.print(" |"); } System.out.println(); switch (i) { case 0: System.out.println(" ---d------e------f---"); break; case 1: System.out.println(" ---g------h------i---"); break; case 2: System.out.println(" ---------------------"); break; } } }

Explanation / Answer

Please rate with 5 stars :)


http://forum.codecall.net/topic/43209-java-source-code-tic-tac-toe-game/#sthash.MYfFbNSo.dpbs