Java Language Programming Exercise 8.9 (Game: Word Game)

Keywords: Java

8.9 (Game: Word Game) In a word-merging game, two players use their own logo (X on one hand and O on the other), filling in a space in the 3x3 grid in turn. When a player appears three identical X or three identical O in the horizontal, vertical or diagonal direction of the grid, the game ends and the player wins. A draw (no winner) is a situation in which no player wins when all the spaces in the grid are filled. Create a program to play # word games. The program prompts two players to choose X and o as their logo. When a player loses a sign, the program reappears the chessboard on the console and determines the game's status (win, draw or continue). Here is a running example:

 

 

 

The following is the implementation code:

  1 /**        fileName:game.java
  2  *         Function: word game
  3  *         Creation date: October 10, 2019  
  4  *         Date of revision: 12 October 2019          
  5  *         mail: xuangliang1@live.com
  6  */
  7 import java.util.Scanner;
  8 
  9  public class game{
 10 
 11      public static void main(String[] args){
 12         Scanner input = new Scanner(System.in);
 13 
 14          //Game player X O
 15         String player1 = "X";
 16         String player2 = "O";
 17 
 18         //Chessboard array
 19         String[][] chess = {{" "," "," "},
 20                             {" "," "," "},
 21                             {" "," "," "}};
 22 
 23         /** End sign, ture for no win or lose results, false for existing players win or draw */
 24         boolean endMask = true; 
 25 
 26         do {
 27             //Print chessboard
 28             chess = emu(chess);
 29             //Player 1 plays chess
 30             System.out.print("Invite players" + player1 + "Input abscissa(1 - 9): ");
 31             int player1Scanner = input.nextInt();
 32 
 33             //Enter the results from Player 1 into the array
 34             while(true){
 35                 if(chess[(player1Scanner - 1)/3][(player1Scanner - 1)%3] != player2){
 36                     chess[(player1Scanner - 1)/3][(player1Scanner - 1)%3] = player1;
 37                     break;
 38                 }
 39                 else{
 40                     System.out.print("Invite players" + player1 + "Re-enter abscissa(1 - 9): ");
 41                     player1Scanner = input.nextInt(); 
 42                 }
 43             }
 44             //Print chessboard
 45             chess = emu(chess);
 46 
 47             //Judging Player 1 Game Result
 48             switch (maskGameResults(chess, player1)){
 49                 case 0: break;
 50                 case 1: System.out.println("Game player " + player1 + " Victory!");
 51                         endMask = false;
 52                         break;
 53                 case -1:    System.out.println("Game draw, please come back!");
 54                             endMask = false;
 55                             break;
 56             }
 57 
 58             //Player 2 plays chess
 59             if(endMask){
 60                 System.out.print("Invite players" + player2 + "Input abscissa(1 - 9): ");
 61                 int player2Scanner = input.nextInt();
 62                 //Enter the results from Player 2 into the array
 63 
 64                 while(true){
 65                     if(chess[(player2Scanner - 1)/3][(player2Scanner - 1)%3] != player1){
 66                         chess[(player2Scanner - 1)/3][(player2Scanner - 1)%3] = player2;
 67                         break;
 68                     }
 69                     else{
 70                         System.out.print("Invite players" + player2 + "Re-enter abscissa(1 - 9): ");
 71                         player2Scanner = input.nextInt(); 
 72                     }
 73                 }
 74 
 75                 chess = emu(chess);
 76                 switch (maskGameResults(chess, player2)){
 77                     case 0: break;
 78                     case 2: System.out.println("Game player " + player2 + " Victory!");
 79                             endMask = false;
 80                             break;
 81                     case -1:    System.out.println("Game draw, please come back!");
 82                                 break;
 83                 }
 84             }
 85             //Print chessboard
 86 
 87         }while(endMask);
 88 
 89      }
 90 
 91      /** Print chessboard */
 92      public static String[][] emu(String [][] chess){
 93         System.out.println("  -------------------");
 94         for(int i = 0; i<3; i++){          
 95             for(int j = 0; j<3; j++ ){
 96                 System.out.printf("  |%3s", chess[i][j]);
 97             }
 98             System.out.println("  |");
 99         System.out.println("  -------------------");
100         }
101         return chess;
102      }
103      
104      /** Judging Player's Win or Lose */
105      public static int maskGameResults(String m[][], String player){
106         //Continuous number of chess pieces
107         int numberRow = 0;
108         if( (m[0][0]==player && m[0][1]==player && m[0][2]==player) ||
109             (m[1][0]==player && m[1][1]==player && m[1][2]==player) ||
110             (m[2][0]==player && m[2][1]==player && m[2][2]==player) ){
111                 if(player == "X")
112                     return 1;
113                 else
114                     return 2;
115         }
116         if( (m[0][0]==player && m[1][0]==player && m[2][0]==player) ||
117             (m[0][1]==player && m[1][1]==player && m[2][1]==player) ||
118             (m[0][2]==player && m[1][2]==player && m[2][2]==player) ){
119                 if(player == "X")
120                     return 1;
121                 else
122                     return 2;
123         }
124         if( (m[0][0]==player && m[1][1]==player && m[2][2]==player) ||
125             (m[0][2]==player && m[1][1]==player && m[2][0]==player) ){
126                 if(player == "X")
127                     return 1;
128                 else
129                     return 2;
130         }
131         for(int i = 0; i<3; i++){
132             for(int j = 0; j<3; j++){
133                 if(m[j][i] == " ")
134                     return 0;
135             }
136         }
137         return -1;
138      }
139  }

Novice just learns java, the algorithm of judging win or lose is dead. If there are better solutions, please leave a message in the comments section.

Posted by Earnan on Sat, 12 Oct 2019 11:51:14 -0700