Introduction to Programming-java language Fifth week programming questions 2 Tic Tac chess (5 points)

Source: Internet
Author: User

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:

    1. X
    2. O
    3. NIL

are all uppercase Letters.

Input sample:

    1. 4
    2. 1 0 0 1
    3. 0 1 0 0
    4. 0 0 1 0
    5. 1 0 0 1

Sample Output:

    1. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.