Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tic Tac Toe program that plays the game with user
- // Renee Waggoner
- // July 26, 2019
- // Special Requirements: None
- package tic_tac_toe;
- 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:
- char choice;
- System.out.println("Entering Input For:" + playerID(player));
- System.out.println("enter in cell [a, b, ... i]");
- choice = scan.next().charAt(0);
- switch(choice)
- {
- case 'a':ttt[0][0] = player;break;
- case 'b':ttt[0][1] = player;break;
- case 'c':ttt[0][2] = player;break;
- case 'd':ttt[1][0] = player;break;
- case 'e':ttt[1][1] = player;break;
- case 'f':ttt[1][2] = player;break;
- case 'g':ttt[2][0] = player;break;
- case 'h':ttt[2][1] = player;break;
- case 'i':ttt[2][2] = player;break;
- }
- }
- boolean gameIsDraw()
- {
- // ******* Details to fill in ************
- // If all ttt entries have been taken return true
- // otherwise return false
- for(int i = 0; i < ttt.length; i++)
- for(int j = 0; j < ttt [i].length; j++)
- if( ttt [i][j] == ' ')
- return false;
- return true;
- }
- 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.
- for(int i = 0; i < 3; i++)
- if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
- return true;
- for(int i = 0; i < 3; i++)
- if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
- return true;
- if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
- return true;
- if(ttt[2][0] == player && ttt[1][1] == player && ttt[0][2] == player)
- return true;
- return false;
- }
- void displayBoard()
- {
- // ******* Details to fill in ************
- // game board spacing
- System.out.println("===============================e");
- System.out.println("--a-----b-----c--");
- System.out.println("| "+ttt[0][0]+" | "+ttt[0][1]+" | "+ttt[0][2]+" |\n");
- System.out.println("--d-----e-----f--");
- System.out.println("| "+ttt[1][0]+" | "+ttt[1][1]+" | "+ttt[1][2]+" |\n");
- System.out.println("--g-----h-----i--");
- System.out.println("| "+ttt[2][0]+" | "+ttt[2][1]+" | "+ttt[2][2]+" |\n");
- System.out.println("-----------------");
- }
- void newgame()
- {
- char currPlayer = player1;
- for(int i = 0; i < 3; i++)
- for(int j = 0; j < 3; j++)
- ttt [i][j] =' ';
- boolean continueOn = true;
- while (continueOn)
- {
- displayBoard();
- if (gameIsDraw())
- {
- System.out.println("******** Game Ends in Draw");
- continueOn = false;
- }
- else
- {
- getPlayerInput(currPlayer);
- if (winner(currPlayer))
- {
- System.out.println("******** We have a winner: " + playerID(currPlayer));
- // calling displayBoard method
- displayBoard();
- continueOn = 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));
- // otherwise prints Bye
- System.out.println("Bye");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement