HW7.9



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的更多相关文章
- HW7.18
public class Solution { public static void main(String[] args) { int[][] m = {{1, 2}, {3, 4}, {5, 6} ...
- HW7.17
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.16
import java.util.Arrays; public class Solution { public static void main(String[] args) { int row = ...
- HW7.15
public class Solution { public static void main(String[] args) { double[][] set1 = {{1, 1}, {2, 2}, ...
- HW7.14
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.13
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.12
import java.util.Scanner; public class Solution { public static void main(String[] args) { double[] ...
- HW7.11
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.10
public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...
随机推荐
- VS2005 VS2008 Manifest 配置问题总结
一.问题 编译某个遗留工程后,运行程序时报错,“由于应用程序的配置不正确,应用程序无法启动.重新安装应用程序可能会解决这个问题.” 查看生成的Manifest文件如下: <?xml versio ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 修改MYSQL数据库表的字符集
MySQL 乱码的根源是的 MySQL 字符集设置不当的问题,本文汇总了有关查看 MySQL 字符集的命令.包括查看 MySQL 数据库服务器字符集.查看 MySQL 数据库字符集,以及数据表和字段的 ...
- hbase 使用备忘
hbase是基于hadoop的,所以hbase服务器必须启动hadoop,这点很重要. 当然hbase其实只用到了dadoop的一个组件 1. 启动hadoop-dfs 在主上执行如下命令,可以把主和 ...
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...
- /etc/bashrc,用户目录下.bashrc有什么区别?
/etc/bashrc,用户目录下.bashrc有什么区别? 一个是针对整个系统所有用户的,一个是针对特定用户的./etc/bashrc修改了以后要重启系统才生效,而用户目录下.bashrc修改了以后 ...
- python set type 集合类型的数据介绍 (set frozenset)
python支持数学中的集合概念,如:通过in,not in 可以检查某元素是否在,不在集合中. python有两种集合类型,set(可以变的,不能哈希,不能用作字典的key),frozenset ...
- 如何写mysql的定时任务
什么是事件: 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行,而触发器是因为某件事件(增删改)触发执行: 查看是否开启: show variables li ...
- 八大Webkit内核浏览器
列举出时下最流行的Webkit内核浏览器,所以我们并不会做出评测和对比.PS:本文列举的浏览器有一部分为IE+Webkit双核浏览器,如果您对其他IE内核浏览器很感兴趣<抛弃数据!用体验和感觉告 ...
- bzoj3157 3516
太神了,被数学题虐了 orz http://m.blog.csdn.net/blog/skywalkert/43970331 这道题关键是抓住m较小的特点,构造递推解决 ; ..,..] of lon ...