2
Kanki (5 Min)
Topic content:
well, That's The tic-tac-chess in the Video. The video said the basic idea, now, you need to realize it all Out.
Your program first reads an integer n, the range is [3,100], which indicates the edge length of the tic chess Board. n=3, for example, represents a 3x3 Chessboard. then, to read n rows, each row n digits, each number is 1 or 0, which in turn represents [0,0] to the [n-1,n-1] position of the Pawn. 1 indicates that x,0 represents o (capital Letter o).
Your program will determine if one of the parties wins, and the winning condition is that there is an entire row or column, or the entire diagonal or the entire counter-diagonal is the same piece. If present, the output represents the winning Letter: x or o (capital letter X or o), and if no one wins, the output is nil (three uppercase letters, The letter I in the middle india).
Note: the distribution of the pieces on the given board may appear in many places where the same piece satisfies the winning condition, but there is no case where both of the pieces win.
Input Format:
A number n representing the size of the chessboard, followed by a number of NXN 0 or 1.
Output format:
One of three outputs:
- X
- O
- NIL
are all uppercase Letters.
Input sample:
- 4
- 1 0 0 1
- 0 1 0 0
- 0 0 1 0
- 1 0 0 1
Sample Output:
- X
Time limit: 500ms Memory limit: 32000kb
Import java.util.Scanner; public class Hello{public static void main (string[] Args) {//TODO auto-generated method Stubscanner in=new Scanner (System . in); int N=in.nextint (); The range is [3,100]int[][] board= new Int[n][n];boolean gotresult=false;int numofx=0;int numof0=0;//read-in moment Chen array for (int i=0;i< Board.length;i++) {for (int j=0;j<board[i].length;j++) {board[i][j]=in.nextint ();}} Judgment line for (int i=0;i<board.length;i++) {for (int j=0;j<board[i].length;j++) {if (board[i][j]==1) {numofx++;} else{numof0++;}} If (numofx==n| | Numof0==n) {gotresult=true;break;} else{numofx=0;numof0=0;}} Judgment column if (!gotresult) {for (int. j=0;j<n;j++) {for (int i=0;i<n;i++) {if (board[i][j]==1) {numofx++;} else{numof0++;}} If (numofx==n| | Numof0==n) {gotresult=true;break;} else{numofx=0;numof0=0;}}} Judge the diagonal if (!gotresult) {for (int i=0;i<n;i++) {if (board[i][i]==1) {numofx++;} else{numof0++;}} If (numofx==n| | Numof0==n) {gotresult=true;} else{numofx=0;numof0=0;}} To determine the inverse diagonal if (!gotresult) {for (int i=0;i<n;i++) {if (board[i][n-i-1]==1) {numofx++;} Else{numof0++;}} If (numofx==n| | Numof0==n) {gotresult=true;} else{numofx=0;numof0=0;}} Output if (gotresult) {if (numofx==n) {System.out.println ("X");} else if (numof0==n) {System.out.println ("0");}} else {System.out.println ("NIL");}}}
Introduction to Programming-java language Fifth week programming questions 2 Tic Tac chess (5 points)