Java井字棋游戏
试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议。
一、棋盘类
package 井字棋游戏;
public class ChessBoard {
private int number;
Person player = new Person(); // 创建棋手
String[][] board = new String[3][3]; // 创建棋盘
// 设置棋子个数
public void setNumber(int number) {
this.number = number;
}
// 获得棋子数
public int getNumber() {
return this.number;
}
// 打印棋盘
public void printBoard() {
for (int i = 0; i < 3; i++) {
System.out.println("-------------");
for (int j = 0; j < 3; j++) {
if (board[i][j] == null)
System.out.printf("| ");
else
System.out.printf("| %s ", board[i][j]);
}
System.out.println("|");
}
System.out.println("-------------");
}
// 判断位置是否合法
public boolean judgement(int row, int column) {
if (board[row][column] == null) //该位置无棋子
return true;
else if (row > 2 || row < 0 || column > 2 || column < 0) //越界
return false;
else //该位置有棋子
return false;
}
// 放置棋子
public boolean putChess(String chess) {
player.chessPlace(chess);
// 若棋子位置合法,则存入数组
if (judgement(player.row, player.column)) {
board[player.row][player.column] = player.chesspiece;
return true;
}
else {
System.out.println("This site has been taken up, please choose another!");
return false;
}
}
// 胜利的条件
public boolean winCondition() {
int i, j;
// 判断行
for (i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != null) {
System.out.printf("%s player won!\n", board[i][0]);
return true;
}
}
//判断列
for (j = 0; j < 3; j++) {
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != null) {
System.out.printf("%s player won!\n", board[0][j]);
return true;
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != null) {
System.out.printf("%s player won!\n", board[0][0]);
return true;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != null) {
System.out.printf("%s player won!\n", board[0][2]);
return true;
}
return false;
}
}
二、棋手类
package 井字棋游戏; import java.util.Scanner;
public class Person {
Scanner s = new Scanner(System.in);
int row, column;
String chesspiece; // 棋子类型
// 选择棋子
public void chooseChess() {
String playerone, playertwo;
do {
System.out.printf("The first player chooses chess(X or O):");
playerone = s.nextLine();
}while(!(playerone.equals("X") || playerone.equals("x") || playerone.equals("O") || playerone.equals("o")));
if (playerone.equals("X") || playerone.equals("x")){
playertwo = "O";
System.out.printf("The first player is %s, the second player is %s\n", playerone,playertwo);
}
else {
playertwo = "X";
System.out.printf("The first player is %s, the second player is %s\n", playerone,playertwo);
}
}
// 选择棋子的位置
public void chessPlace(String chesspiece) {
do {
System.out.printf("Enter a row (1, 2 or 3) for player %s:", chesspiece);
row = s.nextInt() - 1;
s.nextLine();
}while(row < 0 || row > 2);
do {
System.out.printf("Enter a column (1, 2 or 3) for player %s:", chesspiece);
column = s.nextInt() - 1;
s.nextLine();
}while(column < 0 || column > 2);
this.chesspiece = chesspiece;
}
// 选择是否开始下一局
public boolean reStart() {
Scanner s = new Scanner(System.in);
String flag;
System.out.printf("Do you want a new game?(Y or N):");
flag = s.nextLine();
s.close();
if (flag.equals("Y") || flag.equals("y"))
return true;
else
return false;
}
}
三、测试
package 井字棋游戏;
public class Test {
public static void main(String[] args) {
while (true) {
int i = 0;
ChessBoard p = new ChessBoard();
p.player.chooseChess();
p.printBoard();
while(!p.winCondition()) {
if (p.getNumber() % 2 == 0) {
boolean judge = p.putChess("X");
if (!judge) continue; // 如果位置不合法,则重新下
}
else {
boolean judge = p.putChess("O");
if (!judge) continue; // 如果位置不合法,则重新下
}
i++; // 棋子数加一
p.setNumber(i); // 设置棋子数
p.printBoard();
if(p.getNumber() == 9) {
System.out.println("This is a draw!");
break;
}
}
if (!p.player.reStart()) {
System.out.println("Game Over!");
break;
}
}
}
}
效果如下:

Java井字棋游戏的更多相关文章
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- 井字棋游戏升级版 - TopTicTacToe项目 简介
一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的 ...
- C++井字棋游戏,DOS界面版
据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 ...
- JavaFX 井字棋游戏
利用JavaFX设计一个井字棋游戏,其中包括了能够与玩家对战的AI.AI的实现相比五子棋来说要简单得多,可以保证AI在后手情况下绝对不会输,具体实现如下: /* * To change this li ...
- [C++] 井字棋游戏源码
TicTac.h #define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual B ...
- [LeetCode] 348. Design Tic-Tac-Toe 设计井字棋游戏
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- Raptor井字棋游戏
作为大学第一个小作品,记录一下,也给那些想接触到Raptor游戏的人一个小小的参考QAQ至于Raptor的语法和使用,可以参考一下他的帮助手册,看不懂英文的话可以复制放到翻译上看. 以上是主函数 以下 ...
- [Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
随机推荐
- nginx部署静态文件站点
server { listen PORT; #PORT为监听端口 server_name SERVER_NAME; #SERVER_NAME为域名 charset utf-8; autoindex o ...
- Proto3:C++代码生成指南
本章节实际上是介绍Protocol Buffer编译器从给定的protocol定义中生成的C++代码.所有proto2和proto3生成的代码不同之处都会高亮标出 --- 需要注意的是这些不同之处只是 ...
- 2000字谏言,给那些想学Python的人,建议收藏后细看!
1. 这几天陆续收到很多读者.球友的留言.私信,说要怎么学Python?有没有基础的,偏小白的学习方法?我的回答是:等我统一答复. 小胖从不食言,今天就来说说我觉得一个零基础.想转行.一直不得法的人应 ...
- hexo文章编写部分语法总结以及hexo使用
一.hexo的使用 1.1 新建一篇文章 1 $ hexo new [layout] <title> 1.2. 生成静态文件 1 $ hexo generate 可简写为 1 $ hexo ...
- HTTP Continuation or non-HTTP traffic
发现一个 HTTP Continuation or non-HTTP traffic的数据包,之前没有碰到过.不懂其意义,一看长度,显示1460,与TCP segment of a reas ...
- PHP 解决对文件操作的高并发问题
解决方案: 对文件进行加锁时,设置一个超时时间.超时设置为1ms,如果这段时间内没有获得锁,就反复获得,直到获得对文件的操作权为止.如果超市限制已到,就必须马上退出,让出锁让其他进程进行操作. ...
- Docker实战之Consul集群
前言 最近参加了几场 Java 面试,发现大多数的微服务实践还是 Eureka 偏多,鉴于笔者的单位选型 Consul,这里对 Consul 做简单总结. 该篇是 Docker 实战系列的第三篇.传送 ...
- Python——详解collections工具库
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天为大家介绍Python当中一个很好用也是很基础的工具库,叫做collections. collection在英文当中有容器的意思,所以顾 ...
- win10下安装LoadRunner12汉化包
1.前提是已经下载LoadRunner安装文件,及已经安装成功: 安装包: 安装成功后,桌面会出现3个图标: 下面,开始安装汉化包: 1.右键点击“HP_LoadRunner_12.02_Commun ...
- 一文看懂js中元素偏移量(offsetLeft,offsetTop,offsetWidth,offsetHeight)
偏移量(offset dimension) 偏移量:包括元素在屏幕上占用的所有可见空间,元素的可见大小有其高度,宽度决定,包括所有内边距,滚动条和边框大小(注意,不包括外边距). 以下4个属性可以获取 ...