Saturday 21 June 2014

Things You will Learn:

  • Make tictactoe in Java/C#
  • Deciding a winner in TicTacToe
  • TicTacToe pseudo code

Pseudo Code :

  1. Player selects a box i.e '1' 
  2. If that box is already filled with any player symbol gives an error and repeats the current iteration
  3. If its not filled already,that box is filled with the current player's symbol
  4. when the the number of filled boxes is greater than or equal to 5 the decidewinner method is called to check if we have a winner right now continues until all the boxes are filled.

Here is the code :

1:  import java.util.Scanner;  
2:  class TicTacToe{  
3:         
4:       Scanner in = new Scanner(System.in);  
5:         
6:       int choosedbox , iteration ;  
7:         
8:       char player1Symbol = 'x' , player2Symbol = '0' , currentSymbol = 'x' , box[] = {'0','1','2','3','4','5','6','7','8','9'};  
9:      
10:       String p1 = "Player1", p2 = "Player2" , currentPlayer = p1;  
11:          
12:         //*******draws a tic tac toe board on the screen*******  
13:  public void Board(){  
14:                        
15:            System.out.println( " " + box[1] + " | " + box[2] + " | " + box[3] );  
16:            System.out.println( " ___|___|____");    
17:            System.out.println( "  |  |");  
18:            System.out.println( " " + box[4] + " | " + box[5] + " | " + box[6] );   
19:            System.out.println( "____|___|____");  
20:            System.out.println( "  |  |");    
21:            System.out.println( " " + box[7]  + " | " + box[8] + " | " + box[9]);  
22:            System.out.println();  
23:            System.out.println();            
24:         
25:                 }// *********drawying gameboard finished*********  
26:                   
27:            // ********Starts playing********  
28:  public void startPlay() {  
29:              
30:            Board();   // calling method "Board"  
31:               
32:            System.out.println( " The Symbol for player1 is 'x' and for player2 is '0 " ) ;  
33:              
34:            out:                      // titled or named for loop with name out  
35:            for( iteration = 0; iteration < 9; iteration++ )  
36:            {                              //body of the loop starts  
37:            if( iteration % 2 == 0 ){  
38:                 currentSymbol = player1Symbol;  
39:              currentPlayer = p1;         
40:               }  
41:            else  
42:              {    
43:                 currentSymbol = player2Symbol;  
44:                 currentPlayer = p2;    
45:                 }  
46:                   
47:            System.out.println( currentPlayer + " turn please select a box= " );       
48:              
49:            choosedbox = in.nextInt(); //asks the user to enter the box he want to choose  
50:               
51:            if( box[choosedbox] == player1Symbol || box[choosedbox] == player2Symbol )  
52:               {    
53:                  System.out.println( " ERROR,This box is already filled select another box " );  
54:              iteration = iteration - 1 ; //Repeat the current iteration if box selected by the user is already filled   
55:              continue out ;    
56:             }  //continues the loop and do not move to else if statement  
57:              
58:            box[choosedbox] = currentSymbol; // filling the selected box with the symbol  
59:              
60:            Board();  
61:              
62:            if(iteration >= 4) //decideWinner(); is only called when five boxes of the gameboard are filled by symbols of the players  
63:                 decideWinner();      
64:                        
65:            }//end of for loop
66:                      
67:            }//end of method  
68:    
69:  public void decideWinner(){  
70:                        
71:            if( box[1] == box[2] && box[2] == box[3] && box[3] == currentSymbol)  
72:              {    
73:                 System.out.println( currentPlayer + " Wins." );  
74:              iteration = 9;   
75:              }  
76:              
77:            else if( box[4] == box[5] && box[5] == box[6] && box[6] == currentSymbol)  
78:                 {   
79:                 System.out.println( currentPlayer + " Wins." );  
80:                 iteration = 9;   
81:                 }  
82:                   
83:            else if(box[7] == box[8] && box[8] == box[9] && box[9] == currentSymbol)  
84:                 {   
85:                 System.out.println( currentPlayer + " Wins. " );  
86:                 iteration = 9;  
87:                 }  
88:              
89:            else if( box[1] == box[4] && box[4] == box[7] && box[7] == currentSymbol )  
90:              {    
91:                 System.out.println( currentPlayer + " Wins." );  
92:              iteration = 9;  
93:               }  
94:                   
95:            else if( box[2] == box[5] && box[5] == box[8] && box[8] == currentSymbol)  
96:              {    
97:                 System.out.println( currentPlayer + " Wins." );  
98:                 iteration = 9;   
99:                   }  
100:                        
101:            else if( box[3] == box[6] && box[6] == box[9] && box[9] == currentSymbol)  
102:             {    
103:                 System.out.println( currentPlayer + " Wins." );  
104:              iteration = 9;  
105:                }  
106:                        
107:            else if( box[1] == box[5] && box[5] == box[9] && box[9] == currentSymbol)  
108:              {   
109:                 System.out.println( currentPlayer + " Wins." );  
110:                 iteration = 9;  
111:                   }  
112:                   
113:            else if( box[3] == box[5] && box[5] == box[7] && box[7] == currentSymbol)  
114:             {    
115:                 System.out.println( currentPlayer + " Wins." );  
116:                 iteration = 9;    
117:                  }  //finished calculating the winner  
118:              
119:            if( iteration < 9 && iteration > 4 )  
120:              System.out.println( " No Result so Far Continue Playing...... " );  
121:              
122:            else if(iteration == 8)  
123:                 System.out.println( " The Game finished and its a TIE!!!!! " );//prints this if game is Draw  
124:                   
125:       }  
126:       // MAIN Method            
127:  public static void main(String argss[]){  
128:            TicTacToe game = new TicTacToe();  
129:            game.startPlay();  
130:       }  
131:  }  

Explanation : 


  • variable currentSymbol stores the symbol of the active player in it                           


When we run the program "startPlay()" method  is called which prints the board.
In that method there is a for loop which iterates from 0 to less than 9. On line# 37 we check if the iteration is even or odd(even for player1 and odd for player2) if its even the "currentSymbol" is set equal to "player1Symbol" and if its odd "currentSymbol" is set equal to "player2Symbol".
      
   After that the player selects a box to fill and the currentSymbol is  printed in the selected box if the selected box is empty. If the selected box is not empty the current  iteration is repeated and the player is asked again to select a box. Then the board is printed again then. Then the process repeats until the iteration = 4.

Deciding Winner:    

When Iteration becomes Equal to 4(i.e five boxes are filled)then the decidewinner method is called to check if someone Won? In decide Winner Method we have if conditions checking for all winning combinations(i.e (1,2,3),(1,4,7),(4,5,6),(7,8,9),(2,5,8),(3,6,9),(1,5,9),(3,5,7)) . If someone has won then the loop is broken and the message showing that the current player has won is displayed.

You can Download the Source Code by Clicking Below Links

TicTacToe Code in Java

TicTacToe Code in C#

Share this Post With Your Friends!!!

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!