import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         char[][] chessboard = new char[7][7];
         int x, y;

         for(int i = 0; i < 7; i++)
             for(int j = 0; j < 7; j++)
                 chessboard[i][j] = ' ';
         for(int i = 0; i < 7; i += 2)
             for(int j = 0; j < 7; j++)
                 chessboard[i][j] = '-';
         for(int i = 1; i < 7; i += 2)
             for(int j = 0; j < 7; j += 2)
                 chessboard[i][j] = '|';

         display(chessboard);

         while(true)
         {
             System.out.print("Enter a row(1, 2, or 3) for player X: ");
             x = input.nextInt();
             System.out.print("Enter a column(1, 2, or 3) for player X: ");
             y = input.nextInt();
             putChess(chessboard, x, y, 'X');
             display(chessboard);

             if(judge(chessboard) == true)
             {
                 System.out.println("X player won");
                 break;
             }

             System.out.print("Enter a row(1, 2, or 3) for player O: ");
             x = input.nextInt();
             System.out.print("Enter a column(1, 2, or 3) for player O: ");
             y = input.nextInt();
             putChess(chessboard, x, y, 'O');
             display(chessboard);

             if(judge(chessboard) == true)
             {
                 System.out.println("O player won");
                 break;
             }

             if(isFull(chessboard))
             {
                 System.out.println("Draw");
                 break;
             }
         }
     }

     public static void display(char[][] array)
     {
         for(int i = 0; i < 7; i++)
         {
             for(int j = 0; j < 7; j++)
                 System.out.print(array[i][j]);
             System.out.println();
         }
     }

     public static void putChess(char[][] array, int x, int y, char ch)
     {
         array[2 * x - 1][2 * y - 1] = ch;
     }

     public static boolean judge(char[][] array)
     {
         for(int i = 1; i < 7; i += 2)
         {
             if(array[i][1] == array[i][3] && array[i][1] == array[i][5] && array[i][1] != ' ')
                 return true;
             if(array[1][i] == array[3][i] && array[1][i] == array[5][i] && array[1][i] != ' ')
                 return true;
         }
         if(array[1][1] == array[3][3] && array[1][1] == array[5][5] && array[1][1] != ' ')
             return true;
         if(array[5][1] == array[3][3] && array[5][1] == array[1][5] && array[5][1] != ' ')
             return true;
         return false;

     }

     public static boolean isFull(char[][] array)
     {
         for(int i = 0; i < array.length; i++)
             for(int j = 0; j < array[0].length; j++)
                 if(array[i][j] == ' ')
                     return false;
         return true;
     }
 }

HW7.9的更多相关文章

  1. HW7.18

    public class Solution { public static void main(String[] args) { int[][] m = {{1, 2}, {3, 4}, {5, 6} ...

  2. HW7.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. HW7.16

    import java.util.Arrays; public class Solution { public static void main(String[] args) { int row = ...

  4. HW7.15

    public class Solution { public static void main(String[] args) { double[][] set1 = {{1, 1}, {2, 2}, ...

  5. HW7.14

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. HW7.13

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. HW7.12

    import java.util.Scanner; public class Solution { public static void main(String[] args) { double[] ...

  8. HW7.11

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  9. HW7.10

    public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...

随机推荐

  1. Hadoop将过时了?

    http://www.kuqin.com/database/20120715/322528.html Hadoop这个单词如今铺天盖地,几乎成了大数据的代名词.仅仅数年时间,Hadoop从边缘技术迅速 ...

  2. 使用eclipse远程调试Tomcat的方法

    tomcat是一种非常常见的java web应用服务器,有时候服务器可能并不是部署在本地,而是部署在远程其他的机器上,我们用eclispe该如何进行debug调试呢? 1. 在eclispe中新建we ...

  3. QT5.7交叉编译安装到arm(好多系列文章)

    以下采用的系统为ubuntu16.04,开发板为迅为iTOP4412,4.3寸屏. 下载qt5.7源码qt-everywhere-opensource-src-5.7.0.tar.xz http:// ...

  4. Android 自定义对话框使用静态Handler传递参数

    JsdMainDialog.java package com.jsd.demo; import android.app.Activity; import android.content.Context ...

  5. 睡眠--TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE

    http://i.cnblogs.com/EditPosts.aspx?opt=1   Two states are associated with sleeping, TASK_INTERRUPTI ...

  6. nginx负载均衡 - session失效

    最近迷上了Nginx,真实麻雀虽小,五脏俱全..功能实在强大.. nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态.静态页面的分离,可以 ...

  7. How to download apk for google play online?

    http://apps.evozi.com/apk-downloader/ Online APK Downloader http://apkpure.com/ apkpure http://www.c ...

  8. C#中配置文件的使用

    1. 向项目添加app.config文件: 右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”:如果项目以前没有配置文件,则默认的文件名称为“app ...

  9. 网站开发中的相对URL问题--JSP

    问题描述: 入门网站开发时,我们会在相对URL问题上有疑惑.例如,在一个jsp页面中引入css外部文件, <link rel="stylesheet"          hr ...

  10. bzoj3237

    首先我们可以把没有询问过的边处理掉,重构图 当然这样也不影响复杂度 考虑到每次询问要删除的边很少,我们完全可以整体处理 把询问划分成两个集合,在前半部分询问未出现边我们可以整体处理掉,缩点重编号(询问 ...