Simulation games
Game simulation
The two sides of the nine grids take turns.
One Party draws the x symbol and the other party draws the o symbol.
At the beginning, nine grids are empty.
The program displays the current situation, prompting one party to enter the sub-location, then displaying the situation, and then prompting the other party.
When a piece is in a straight line, this party wins!
For example:
Initial:
___
___
___
O input position: 1, 1
O __
___
___
X Input position: 2, 2
O __
_ X _
___
When one party has three pawns connected to a straight line or diagonal line, the party wins.
Standard answer:
Import java. util. *; class MyChess {private char [] [] data; // chess and board private boolean side; // which party is playing the game? true: o, false: x-party private static external scan = new external (System. in); public MyChess () {data = new char [3] [3]; side = true;} public void show () {for (int I = 0; I <3; I ++) {for (int j = 0; j <3; j ++) {if (data [I] [j] = 0) System. out. print ('_'); elseSystem. out. print (data [I] [j]); System. out. print ("");} System. out. println () ;}} p Ublic boolean isEnd () {// check whether the outcome is exceeded if (judge ()> 0) return true; // check whether the outcome can continue for (int I = 0; I <3; I ++) for (int j = 0; j <3; j ++) {if (data [I] [j] = 0) return false;} return true;} // victory and defeat referee private int judge () {// detect crossline for (int k = 0; k <3; k ++) {if (data [k] [0] = data [k] [1] & data [k] [0] = data [k] [2]) {if (data [k] [0] = 'O') return 1; // if (data [k] [0] = 'X ') return 2; // x-Party victory }}// detect the vertical line for (int k = 0; k <3; k ++) {if (data [0] [k] = Data [1] [k] & data [0] [k] = data [2] [k]) {if (data [0] [k] = 'O') return 1; // if (data [0] [k] = 'X ') return 2; // x-Party victory }}// check the diagonal line if (data [0] [0] = data [1] [1] & data [0] [0] = data [2] [2]) {if (data [0] [0] = 'O') return 1; // if (data [0] [0] = 'X ') return 2; // x-Party victory} if (data [0] [2] = data [1] [1] & data [0] [2] = data [2] [0]) {if (data [0] [2] = 'O') return 1; // if (data [0] [2] = 'X ') return 2; // x-Party victory} return 0;} // display Show the competition result public void showResult () {switch (judge () {case 0: System. out. println ("Draw! "); Break; case 1: System. out. println (" Victory! "); Break; case 2: System. out. println (" x wins! "); Break ;}} public void play () {for (;) {char side_char = side? 'O': 'X'; System. out. print ("" + side_char + "Enter the position (row, column):"); String s = scan. nextLine (); try {String [] ss = s. split (","); int row = Integer. parseInt (ss [0])-1; // The row number int col = Integer. parseInt (ss [1])-1; // if (data [row] [col]! = 0) continue; // data [row] [col] = side_char already exists in this position; // records the sub-side =! Side; break;} catch (Exception e) {}}} public class MyTest {public static void main (String [] args) {// o x fill game/* Initial: _ o input location: 1, 1, o _ x input location: 2, 2 o _ x _ when one party has three pawns connected to a straight line or diagonal line, the party wins */MyChess a = new MyChess (); for (;) {. show (); // display the situation if (. isEnd () {. showResult (); break;}. play (); // continue playing games }}}