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 ...
随机推荐
- 从iPhone X到三星S9,为何现在山寨还能如此肆无忌惮?
X到三星S9,为何现在山寨还能如此肆无忌惮?" title="从iPhone X到三星S9,为何现在山寨还能如此肆无忌惮?"> 曾几何时,以"土豪金&qu ...
- Python学习笔记(四)函数式编程
高阶函数(Higher-order function) Input: 1 abs Output: 1 <function abs> Input: 1 abs(-10) Output: 1 ...
- 用hugo建博客的记录 · 老张不服老
前后累计折腾近6个小时,总算把搭建hugo静态博客的整个过程搞清楚了.为什么用了这么久?主要还是想偷懒,不喜欢读英文说明.那就用中文记录一下过程吧.还是中文顺眼啊. 某日发现自己有展示些东西给外网的需 ...
- 一个很实用的css技巧简析
我是小雨小雨,专注于更新有趣.实用内容的小伙,如果内容对大家有一点帮助,那么就请动动手指,给个关注.点赞支持一下吧. ^ - ^ 序言 前两天接到一个需求,其中包括一个有序的列表,我们今天就来看看这个 ...
- 网络地址转换NAT与端口地址转换PAT
网络地址转换NAT与端口地址转换PAT 一.网络地址转换 NAT (Network Address Translation) 1.1.网络地址转换简介 需要在专用网(内网)连接到因特网的路由器上安装 ...
- 初学Qt——QTableView+QSqlqueryModel
我们在显示报表时可以用到上面两个类来实现,QTableView负责对视图显示:QSqlqueryModel则负责数据模块. 这里数据查询使用QSqlqueryModel主要是这个类可以通过自己写的查询 ...
- python爬虫所遇问题列举
1.通过python socket库来构造请求报文,向服务器发送图片请求时 (1)图片在浏览器请求头中的remote address信息跟通过python socket输出远程连接地址和端口号不一致 ...
- java网络编程——socket实现简单的CS会话
还记得当年学计网课时用python实现一个简单的CS会话功能,这也是学习socket网络编程的第一步,现改用java重新实现,以此记录. 客户端 import java.io.*; import ja ...
- 利用 Rize 来进行 UI 测试或 E2E 测试
之前我曾经在<Rize - 一个可以让你简单.优雅地使用 puppeteer 的 Node.js 库>一文简单介绍过 Rize 这个库.当时仅仅是介绍这个库本身,关于如何使用,我没有给太多 ...
- Linux中软连接和硬连接的区别
首先,我们要清楚符号链接的目的,在不改变原目录/文件的前提下,起一个方便的别名(在这起个别名,让我想到前阶段学C里typedef也是起别名的). 1.软连接就相当于windows的快捷方式.例如:ln ...