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. STM32移植UCGUI3.90笔记

    在MDK环境下,终于将3.90版本的UCGUI移植到STM32下了,在网上看到的都是例程代码,很少看到有关于在STM32下移植UCGUI的教程方法,为了方便大家,特写此移植方法,大家可以借鉴(有错误之 ...

  2. jmeter 测试java协议经验总结

    对java协议的良好支持,是jmeter比loadrunner优秀的地方,但是坑也不少,本文将相关点都整理下来备忘 一. 依赖的jar包 使用IDE开发jemter java协议脚本时,需要导入以下几 ...

  3. SaaS系列介绍之四:我国SaaS市场发展

    1 引言 那些没有经验的问题解决者们,几乎无一例外,都是去匆忙地寻找解决办法,而不是先给要解决的问题下定义.                                               ...

  4. Ubuntu 12.04安装字体

    http://www.2cto.com/os/201210/160645.html 安装方法,终端输入:  $ sudo thunar /usr/share/fonts/truetype  待ture ...

  5. IOS - DatePicker的使用

    UIDatePicker *picker = [[UIDatePicker alloc] init]; picker.datePickerMode = UIDatePickerModeDate; pi ...

  6. ENVI5.1安装破解教程

    原文地址:  ENVI5.1安装破解_百度经验 http://jingyan.baidu.com/article/020278118b5ded1bcd9ce57a.html   ENVI5.1_x86 ...

  7. PHP设计模式浅析

    工厂模式 提到的最多, 用途也最广. 简单说就是: 定义一个用户创建对象的接口. 把创建对象的过程封装起来. 工厂类是指包含一个专门用来创建其他对象的方法的类,工厂类在多态性编程实践中是至关重要的,它 ...

  8. RabbitMQ安装和配置

    RabbitMQ: MQ:message queue.MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来 ...

  9. css揭秘之按钮的实现技巧

    <html> <title>css</title> <style> button{ padding: .3em .8em; border: 1px so ...

  10. 结构体UT_LIST_ADD_LAST

    使用 UT_LIST_ADD_LAST(list, buf_pool->free, (&block->page)); /****************************** ...