Tuesday 27 June 2017

Simple Game In Java

Tic-Tac-Toe  Game In Java


I created simple [O,X] Tic-Tac-Toe Game in java. It is very simple to understand and design.Lets Go through it.


1.Player  Class :
import java.util.Scanner;

public class Player {
     private String name;
     private MarkType mark;
     private int row, col;
     Scanner sc = new Scanner(System.in);
    
     public Player(String name, MarkType mark) {
           this.mark = mark;
           this.name = name;
     }

     public String getName() {
           return name;
     }

     public MarkType getMark() {
           return mark;
     }

     public void nextMove() {

           System.out.println(this.getName() + " enter position for " + this.getMark());
           row = sc.nextInt();
           col = sc.nextInt();

     }

     public int getRow() {
           return row - 1;
     }

     public int getCol() {
           return col - 1;
     }
}

2.Board  Class :

public class Board {
     private MarkType board[][];
     private int bsize;

     public Board(int bsize) {
           this.bsize = bsize;
           board = new MarkType[bsize][bsize];
     }

     public int getBoardSize() {
           return bsize;
     }

     public void updateBoard(int row, int col, MarkType mark) {
           if (row > bsize || col > bsize) {
                throw new ArrayIndexOutOfBoundsException();
           } else if (board[row][col] == MarkType.$) {
                board[row][col] = mark;
           } else {
                throw new InvalidPosition("Enter valid position");
           }
     }

     public void initialize() {
           for (int row = 0; row < bsize; row++)
                for (int col = 0; col < bsize; col++)
                     board[row][col] = MarkType.$;
     }

     public boolean isWinner(int passedRow, int passedCol, MarkType mark) {
           for (int col = 0; col < bsize; col++) {
                if (board[passedRow][col] != mark)
                     break;
                if (col == bsize - 1) {
                     return true;
                }
           }

           for (int row = 0; row < bsize; row++) {
                if (board[row][passedCol] != mark)
                     break;
                if (row == bsize - 1) {
                     return true;
                }
           }

           if (passedRow == passedCol) {
                for (int row = 0; row < bsize; row++) {
                     if (board[row][row] != mark)
                           break;
                     if (row == bsize - 1) {
                           return true;
                     }
                }
           }

           if (passedRow + passedCol == bsize - 1) {
                for (int index = 0; index < bsize; index++) {
                     if (board[index][(bsize - 1) - index] != mark)
                           break;
                     if (index == bsize - 1) {
                           return true;
                     }
                }
           }
           return false;
     }

     public void display() {
           for (int row = 0; row < bsize; row++) {
                for (int col = 0; col < bsize; col++) {
                     System.out.print(" " + board[row][col] + " ");
                     if (col != bsize - 1) {
                           System.out.print("|");
                     }
                }
                System.out.println();
                if (row != bsize - 1) {
                     for (int col = 0; col < bsize; col++)
                           System.out.print("--- ");
                }
                System.out.println();
           }
     }

}

3.MarkType  Enum:
public enum MarkType {

     X, O, $;

}


4. TicTacToe Class :

public class TicTacToe {

     private Player player1;
     private Player player2;
     private Board board;

     public TicTacToe(Player player1, Player player2, int bsize) {
           this.player1 = player1;
           this.player2 = player2;
           board = new Board(bsize);
     }

     public void startGame() {
           board.initialize();
           board.display();
           boolean flag = false, flag2 = false;
           int moveCount = 0;
           while (moveCount < board.getBoardSize() * board.getBoardSize()) {
                if (!flag) {
                     try {
                           player1.nextMove();
                           board.updateBoard(player1.getRow(), player1.getCol(), player1.getMark());
                           flag = true;
                           board.display();
                           if (board.isWinner(player1.getRow(), player1.getCol(), player1.getMark())) {
                                break;
                           }
                     } catch (InvalidPosition ip) {
                           System.out.println(ip.getMessage());
                     } catch (ArrayIndexOutOfBoundsException exe) {
                           System.out.println("Enter Valid Position");
                     }
                } else {
                     try {
                           player2.nextMove();
                           board.updateBoard(player2.getRow(), player2.getCol(), player2.getMark());
                           flag = false;
                           board.display();
                           if (board.isWinner(player2.getRow(), player2.getCol(), player2.getMark())) {
                                break;
                           }
                     } catch (InvalidPosition ip) {
                           System.out.println(ip.getMessage());
                     } catch (ArrayIndexOutOfBoundsException ex) {
                           System.out.println("Enter Valid Question");
                     }
                }
                moveCount++;
           }
           if (moveCount == board.getBoardSize() * board.getBoardSize()) {
                flag2 = true;
           }
           if (flag == false && flag2 == false) {
                System.out.println(player2.getName() + " is the winner.");
           } else if (flag == true && flag2 == false) {
                System.out.println(player1.getName() + " is the winner.");
           } else {
                System.out.println("Draw");
           }
     }

}


5. InvalidPosition Class :
public class InvalidPosition extends RuntimeException {

     public InvalidPosition(String message) {

           super(message);
     }
}



6. TicTacToeTest Class
public class TicTacToeTest {
     public static void main(String[] args) {
           Scanner scan = new Scanner(System.in);
           int ch;
           do {
                enterInfo();
                String player = scan.next();
                Player player1 = new Player(player, MarkType.X);

                enterInfo();
                player = scan.next();
                Player player2 = new Player(player, MarkType.O);

                System.out.println("Enter size of board");
                int boardSize = scan.nextInt();

                TicTacToe tictactoe = new TicTacToe(player1, player2, boardSize);
                tictactoe.startGame();

                System.out.println("\nEnter the choice 1 for New Game 0 for Exit ");
                ch = scan.nextInt();
           } while (ch != 0);
           scan.close();
     }

     private static void enterInfo() {
           System.out.println("Enter Name of the player");

     }

}

OUTPUT :
Enter Name of the player
sanket
Enter Name of the player
san
Enter size of board
3
 $ | $ | $
--- --- ---
 $ | $ | $
--- --- ---
 $ | $ | $

sanket enter position for X
1
1
 X | $ | $
--- --- ---
 $ | $ | $
--- --- ---
 $ | $ | $

san enter position for O
2
1
 X | $ | $
--- --- ---
 O | $ | $
--- --- ---
 $ | $ | $

sanket enter position for X
2
2
 X | $ | $
--- --- ---
 O | X | $
--- --- ---
 $ | $ | $

san enter position for O

3
1
 X | $ | $
--- --- ---
 O | X | $
--- --- ---
 O | $ | $

sanket enter position for X

2
2
Enter valid position
sanket enter position for X
2
3
 X | $ | $
--- --- ---
 O | X | X
--- --- ---
 O | $ | $

san enter position for O
3
3
 X | $ | $
--- --- ---
 O | X | X
--- --- ---
 O | $ | O

sanket enter position for X
3
2
 X | $ | $
--- --- ---
 O | X | X
--- --- ---
 O | X | O

san enter position for O
1
3
 X | $ | O
--- --- ---
 O | X | X
--- --- ---
 O | X | O

Draw

Enter the choice 1 for New Game 0 for Exit


Keep Learning !!!!

No comments:

Post a Comment

Spring-App

Application In Spring Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developi...