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
- {
- // setting size of board with char array named ttt
- 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));
- // setting all possible options for a draw
- while(true) {
- System.out.print("enter in cell [a, b, ... i]");
- choice = scan.next().charAt(0);
- int choiceTaken = 0;
- switch(choice)
- {
- case 'a':
- if(ttt[0][0] != player1 || ttt[0][0] != player2) {
- ttt[0][0] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[0][0]));
- }
- break;
- case 'b':
- if(ttt[0][1] != player1 || ttt[0][1] != player2) {
- ttt[0][1] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[0][1]));
- }
- break;
- case 'c':
- if(ttt[0][2] != player1 || ttt[0][2] != player2) {
- ttt[0][2] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[0][2]));
- }
- break;
- case 'd':
- if(ttt[1][0] != player1 || ttt[1][0] != player2) {
- ttt[1][0] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[1][0]));
- }
- break;
- case 'e':
- if(ttt[1][1] != player1 || ttt[1][1] != player2) {
- ttt[1][1] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[1][1]));
- }
- break;
- case 'f':
- if(ttt[1][2] != player1 || ttt[1][2] != player2) {
- ttt[1][2] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[1][2]));
- }
- break;
- case 'g':
- if(ttt[2][0] != player1 || ttt[2][0] != player2) {
- ttt[2][0] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[2][0]));
- }
- break;
- case 'h':
- if(ttt[2][1] != player1 || ttt[2][1] != player2) {
- ttt[2][1] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[2][1]));
- }
- break;
- case 'i':
- if(ttt[2][2] != player1 || ttt[2][2] != player2) {
- ttt[2][2] = player;
- choiceTaken = 1;
- }else {
- System.out.println("Slot is already chosen by "+ playerID(ttt[2][2]));
- }
- break;
- default:
- System.out.println("Please enter a valid slot");
- }
- if(choiceTaken == 1) {
- 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
- {
- // otherwise continue this pattern for play turns
- 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