TXT

FiveChess

By Mario Alexander,2014-08-04 20:12
12 views 0
FiveChess

    package com.tarena.exam.fivechess;

import java.util.Scanner;

    /**

     * Îå×ÓÆåÓÎÏ?(?ØÖÆÌ??æ??)

     */

    public class FiveChess {

     static char[][] chess = new char[16][16];

     static boolean isBlack = true;

    //alt + /

     public static void main(String[] args) {

     for (int i = 0; i < chess.length; i++) {

     for (int j = 0; j < chess[i].length; j++) {

     chess[i][j] = '*';

     }

     }

     Scanner s = new Scanner(System.in);

     printChess();

     while (true) {

     System.out.println("Çë" + (isBlack ? "ºÚ" : "?×") +

    "??Âä×Ó?º");

     String str = s.next();

     int r = fromCharToInt(str.charAt(0));

     int c = fromCharToInt(str.charAt(1));

     if (chess[r][c] != '*') {

     System.out.println("?ÃÎ?ÖÃÒÑ??ÓÐÆå×Ó??ÇëÖØÐÂÊäÈë??");

     continue;

     } else {

     chess[r][c] = isBlack ? '@' : 'O';

     printChess();

     if (wasWin(r, c)) {

     System.out.println((isBlack ? "ºÚ" : "?×") + "???ñÊ???");

     break;

     }

     }

     isBlack = !isBlack;

     }

     }

     public static void printChess() {

     for (int i = 0; i < chess.length; i++) {

     if (i < 10) {

     System.out.print(" " + i);

     } else {

     System.out.print(" " + (char) ('a' + i - 10));

     }

     }

     System.out.println();

     for (int i = 0; i < chess.length; i++) {

     if (i < 10) {

     System.out.print(i + " ");

     } else {

     System.out.print((char) ('a' + i - 10) + " ");

     }

     for (int j = 0; j < chess[i].length; j++) {

     System.out.print(chess[i][j] + " ");

     }

     System.out.println();

     }

     }

     public static int fromCharToInt(char c) {

     if (c >= '0' && c <= '9') {

     return c - '0';

     } else {

     return c - 'a' + 10;

     }

     }

     public static boolean wasWin(int r, int c) {

     return wasWinAtV(r, c) || wasWinAtH(r, c) || wasWinAtLD(r, c)

     || wasWinAtRD(r, c);

     }

     public static boolean wasWinAtV(int r, int c) {

     char ch = isBlack ? '@' : 'O';

     int i = c;

     while (i >= 0 && chess[r][i] == ch) {// Ïò×óÕÒµ?µÚÒ??ö??ÊÇchµÄ×Ö?û

     i--;

     }

     int num = 0;

     i++;

     while (i < chess.length && chess[r][i] == ch) {

     num++;

     i++;

     }

     return num >= 5;

     }

     public static boolean wasWinAtH(int r, int c) {

     char ch = isBlack ? '@' : 'O';

     int i = r;

     while (i >= 0 && chess[i][c] == ch) {// Ïò×óÕÒµ?µÚÒ??ö??ÊÇchµÄ×Ö?û

     i--;

     }

     int num = 0;

     i++;

     while (i < chess.length && chess[i][c] == ch) {

     num++;

     i++;

     }

     return num >= 5;

     }

     public static boolean wasWinAtLD(int r, int c) {

     char ch = isBlack ? '@' : 'O';

     int i = r;

     int j = c;

     while (i >= 0 && j < chess.length && chess[i][j] == ch) {//

    Ïò×óÕÒµ?µÚÒ??ö??ÊÇchµÄ×Ö?û

     i--;

     j++;

     }

     int num = 0;

     i++;

     j--;

     while (i < chess.length && j >= 0 && chess[i][j] == ch) {

     num++;

     i++;

     j--;

     }

     return num >= 5;

     }

     public static boolean wasWinAtRD(int r, int c) {

     char ch = isBlack ? '@' : 'O';

     int i = r;

     int j = c;

     while (i >= 0 && j >= 0 && chess[i][j] == ch) {//

    Ïò×óÕÒµ?µÚÒ??ö??ÊÇchµÄ×Ö?û

     i--;

     j--;

     }

     int num = 0;

     i++;

     j++;

     while (i < chess.length && j < chess.length && chess[i][j] ==

    ch) {

     num++;

     i++;

     j++;

     }

     return num >= 5;

     }

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com