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 ...
随机推荐
- 码海拾遗:strcpy()、strncpy()和strcpy_s()区别
1.strcpy() 原型:char *strcpy(char *dst,const char *src) 功能:将以src为首地址的字符串复制到以dst为首地址的字符串,包括'\0'结束符,返回ds ...
- App自动化测试方案
App自动化测试方案 1.1 概述 什么是App自动化?为什么要做App自动化? App自动化是指给 Android或iOS上的软件应用程序做的自动化测试. 手工测试和自动化测试的对比如下: 手工测 ...
- WEB前端工程师简历
一个热爱前端的工程师 关于我 我的作品 ZENRON 关于我 求职意向 作品集 技术掌握 我的经历 联系我 关于我 英语/CET-4 坐标/苏州 状态/求职 我叫Zenron, 现居住苏州, 是一名前 ...
- GeoMesa-单机搭建
系统安装 CentOS部署 新建虚拟电脑 类型:Linux 版本:Red Hat(64-bit) 创建虚拟硬盘 [x] 动态分配(磁盘占用较小) [ ] 固定大小(使用起来较快) 安装设置(设置roo ...
- 马云也看好!VR在2016年能否得到质变
在2015年10月底有消息传出,国外虚拟现实公司Magic Lea的新一轮融资吸引到中国互联网巨鳄--阿里巴巴公司的关注.据透露,阿里巴巴极有可能为Magic Lea投资2亿美元.这虽然是阿里巴巴第一 ...
- [python每日一练]--0012:敏感词过滤 type2
题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...
- 前端每日实战:1# 视频演示如何用纯 CSS 创作一个按钮文字滑动特效
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/zhang-ou/pen/GdpPLE 可交互视频教程 此视频 ...
- Java Opencv 实现 中值滤波器
原理 Note 以下原理来源于Richard Szeliski 的著作 Computer Vision: Algorithms and Applications 以及 Learning OpenCV ...
- NoVNC API 文档翻译
原文地址:https://github.com/novnc/noVNC/blob/master/docs/API.md 时间:2019-05-21 noVNC API The interfac ...
- golang的sync.WaitGroup使用示例
下面一段代码 len(m) 不一定会打印为 10,为什么?.如果想要 len(m) 打印为 10,应该怎么修改代码? func main() { const N = 10 m := make(map[ ...