zoukankan      html  css  js  c++  java
  • 3*3井字棋

     1 package opp;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Main {
     6     static int dis[][] = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 }, { -1, -1 }, { 1, 1 }, { -1, 1 }, { 1, -1 } };
     7     public static void main(String[] args) {
     8         Scanner cin = new Scanner(System.in);
     9         char[][] table = new char[10][10];
    10         char ch;int cnt=0;
    11         while (true) {
    12             int row, col;
    13             if(cnt%2==0)
    14                ch = 'X';
    15             else
    16                 ch='0';
    17             System.out.print("Enter a row (0,1,or 2) for player " + ch + ": ");
    18             row = cin.nextInt();
    19             System.out.print("Enter a column (0,1,or 2) for player " + ch + ": ");
    20             col = cin.nextInt();
    21             table[row * 2][col * 3] = ch;
    22             Table(table);
    23             cnt++;
    24             if(solSatSearch(table,4,6)==true||solSatSearch(table,6,6)==true||solSatSearch(table,2,6)==true||solSatSearch(table,4,3)==true||solSatSearch(table,4,9)==true) {
    25                 System.out.println(ch+" player won");
    26                 break;
    27             }
    28         }
    29     }
    30     public static void Table(char[][] table) {
    31         for (int i = 1; i <= 7; i++) {
    32             for (int j = 1; j <= 10; j++) {
    33                 if (i % 2 == 1) {
    34                     System.out.print("-");
    35                 } else {
    36                     if (j % 3 == 1) {
    37                         System.out.print("|");
    38                     } else {
    39                         if (table[i][j] != 'u0000') {
    40                             System.out.print(table[i][j]);
    41                         } else {
    42                             System.out.print(" ");
    43                         }
    44                     }
    45                 }
    46             }
    47             System.out.println("");
    48         }
    49     }
    50     public static boolean solSatSearch(char[][] str,int row,int col) {
    51         for (int i = 0; i < 8; i+=2) {
    52             int res = 0;
    53             for (int j = 0; j < 2; j++) {
    54                 int x = row + dis[i + j][0] * 2;
    55                 int y = col + dis[i + j][1] * 3;
    56                 if (judgeRowCol(x, y) && str[x][y] == str[row][col]&&str[row][col]!='u0000') {
    57                     res++;
    58                 }
    59             }
    60             if(res==2)
    61                 return true;
    62         }
    63         return false;
    64 
    65     }
    66     private static boolean judgeRowCol(int x, int y) {
    67         if (x < 0 || x > 6 || y < 0 || y > 9)
    68             return false;
    69         return true;
    70     }
    71 }
  • 相关阅读:
    day84
    模型层之单表操作
    Django的模板层
    Django框架导读
    创建Django项目
    名称空间2.0path
    js基础之BOM和DOM
    LG5003 跳舞的线
    20191003 「HZOJ NOIP2019 Round #8」20191003模拟
    LG3092 「USACO2013NOV」No Change 状压DP
  • 原文地址:https://www.cnblogs.com/moomcake/p/11601302.html
Copyright © 2011-2022 走看看