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( ...
随机推荐
- oracle SQLserver 函数
1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) S:select ceiling(-1.001) value ...
- HDU 1062 Text Reverse
题意 : 给出你一个句子,让你把句子中每个单词的字母顺序颠倒一下输出. 思路 : 用栈即可,就是注意原来在哪儿有空格就要输出空格. //hdu1062 #include <iostream> ...
- 推荐牛X的一本JS书
主要是看阮一峰的教程时,他参考书目里有这一本中文的, 找来一看,果然高.. 练习一下. function Base(name) { this.name = name; this.getName = f ...
- php简单文件上传类
<?php header("Content-Type:text/html; charset=utf-8"); if($_POST['submit']){ $upfiles = ...
- WCF之各种WCF引用方式
写在开头:本文内容来自 WCF全面解析中的一个经典例子,如果你已经看过了,那么可以忽略本文,本文旨在和大家分享不一样的WCF使用方法. 准备工作: 1.创建解决方案WCFService(当然名字可以任 ...
- 123. Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 最短路径算法之二——Dijkstra算法
Dijkstra算法 Dijkstra算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 注意该算法要求图中不存在负权边. 首先我们来定义一个二维数组Edge[MAXN][MAXN]来存储 ...
- SVN使用方法总结
SVN使用方法 SVN版本管理模式:http://www.cnblogs.com/newstar/archive/2011/01/04/svn.html (集中式-trunk和分散式-branch ...
- Vim的撤销与重做
命令模式下 u:撤销 Ctrl+r:重做(撤销撤销)
- HashMap Hashtable ArrayList HashSet
一.散列 1. HashMap 1) hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key ...