Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Tic Tac Toe:
- public class TicTacToe
- {
- private static String [] board;
- public TicTacToe()
- {
- board = new String [9];
- for(int i=0; i<board.length;i++)
- {
- board[i] = " ";
- }
- }
- public static void move(int player, int tile)
- {
- if(player == 1)
- board[tile-1] = "X";
- else
- board[tile-1] = "O";
- }
- public static boolean legal(int tile)
- {
- if(board[tile-1] == " ")
- return true;
- else
- return false;
- }
- public static boolean win1()
- {
- if(board[0]=="X" && board[1]=="X" && board[2]=="X")
- return true;
- else if(board[3]=="X" && board[4]=="X" && board[5]=="X")
- return true;
- else if(board[6]=="X" && board[7]=="X" && board[8]=="X")
- return true;
- else if(board[0]=="X" && board[4]=="X" && board[8]=="X")
- return true;
- else if(board[2]=="X" && board[4]=="X" && board[6]=="X")
- return true;
- else if(board[0]=="X" && board[3]=="X" && board[6]=="X")
- return true;
- else if(board[1]=="X" && board[4]=="X" && board[7]=="X")
- return true;
- else if(board[2]=="X" && board[5]=="X" && board[8]=="X")
- return true;
- else
- return false;
- }
- public static boolean win2()
- {
- if(board[0]=="O" && board[1]=="O" && board[2]=="O")
- return true;
- else if(board[3]=="O" && board[4]=="O" && board[5]=="O")
- return true;
- else if(board[6]=="O" && board[7]=="O" && board[8]=="O")
- return true;
- else if(board[0]=="O" && board[4]=="O" && board[8]=="O")
- return true;
- else if(board[2]=="O" && board[4]=="O" && board[6]=="O")
- return true;
- else if(board[0]=="O" && board[3]=="O" && board[6]=="O")
- return true;
- else if(board[1]=="O" && board[4]=="O" && board[7]=="O")
- return true;
- else if(board[2]=="O" && board[5]=="O" && board[8]=="O")
- return true;
- else
- return false;
- }
- public static boolean tie()
- {
- for(int i=0; i<board.length; i++)
- {
- if(board[i] == " ")
- return false;
- }
- return true;
- }
- public static String youWin(int player)
- {
- return "Congratulations Player "+player+", you won!";
- }
- public static String noWinner()
- {
- return "Sorry, there was a tie. No one wins.";
- }
- public static void printBoard()
- {
- System.out.println("| "+board[0]+" | "+board[1]+" | "+board[2]+" |");
- System.out.println("-------------");
- System.out.println("| "+board[3]+" | "+board[4]+" | "+board[5]+" |");
- System.out.println("-------------");
- System.out.println("| "+board[6]+" | "+board[7]+" | "+board[8]+" |");
- System.out.println("-------------");
- }
- }
- Tic Tac Toe Client:
- import java.util.*;
- public class TicTacToeClient
- {
- public static void main(String [] args)
- {
- TicTacToe game = new TicTacToe();
- boolean player1Win = false;
- boolean player2Win = false;
- boolean tie = false;
- System.out.println("Hello and welcome to Tic Tac Toe!");
- while(!player1Win && !player2Win && !tie)
- {
- Scanner console = new Scanner(System.in);
- System.out.println("Player 1 it is your turn...");
- game.printBoard();
- System.out.print("What is your move? (1-9) > ");
- int input = console.nextInt();
- if(game.legal(input))
- game.move(1,input);
- else
- {
- System.out.print("That move isn't legal. please make another. > ");
- input = console.nextInt();
- game.move(1,input);
- }
- if(game.win1())
- player1Win = true;
- if(game.tie())
- tie = true;
- System.out.println("Player 2 it is your turn...");
- game.printBoard();
- System.out.print("What is your move? (1-9) > ");
- input = console.nextInt();
- if(game.legal(input))
- game.move(2,input);
- else
- {
- System.out.print("That move isn't legal. please make another. > ");
- input = console.nextInt();
- game.move(2,input);
- }
- if(game.win2())
- player1Win = true;
- if(game.tie())
- tie = true;
- }
- if(player1Win)
- System.out.println(game.youWin(1));
- else if(player2Win)
- System.out.println(game.youWin(2));
- else if(tie)
- System.out.println(game.noWinner());
- else
- System.out.println("Error");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement