试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议。

一、棋盘类

 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井字棋游戏的更多相关文章

  1. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  2. 井字棋游戏升级版 - TopTicTacToe项目 简介

    一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的 ...

  3. C++井字棋游戏,DOS界面版

    据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 ...

  4. JavaFX 井字棋游戏

    利用JavaFX设计一个井字棋游戏,其中包括了能够与玩家对战的AI.AI的实现相比五子棋来说要简单得多,可以保证AI在后手情况下绝对不会输,具体实现如下: /* * To change this li ...

  5. [C++] 井字棋游戏源码

    TicTac.h #define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual B ...

  6. [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 ...

  7. [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 ...

  8. Raptor井字棋游戏

    作为大学第一个小作品,记录一下,也给那些想接触到Raptor游戏的人一个小小的参考QAQ至于Raptor的语法和使用,可以参考一下他的帮助手册,看不懂英文的话可以复制放到翻译上看. 以上是主函数 以下 ...

  9. [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 ...

随机推荐

  1. iOS开发之多线程(NSThread、NSOperation、GCD)

    整理一些多线程相关的知识. 并行 & 并发 1.并行:并行是相对于多核而言的,几个任务同时执行.2.并发:并发是相对于单核而言的,几个任务之间快速切换运行,看起来像是"同时" ...

  2. kubernetes集群中的pause容器

    昨天晚上搭建好了k8s多主集群,启动了一个nginx的pod,然而每启动一个pod就伴随这一个pause容器,考虑到之前在做kubelet的systemd unit文件时有见到: 1 2 3 4 5 ...

  3. PHP创建缓存文件

    文件操作类 <?php /** 文件操作类 */ class FileIO { /** * 读取目录 * @param string $dirPath dir名称 * @return array ...

  4. 一个很实用的css技巧简析

    我是小雨小雨,专注于更新有趣.实用内容的小伙,如果内容对大家有一点帮助,那么就请动动手指,给个关注.点赞支持一下吧. ^ - ^ 序言 前两天接到一个需求,其中包括一个有序的列表,我们今天就来看看这个 ...

  5. 如何通过学校系统漏洞注册到 @edu.cn 邮箱账号?

    此文章仅针对我自己学校的系统进行分析,并不代表所有学校的系统都是如此. 我们学校比较"抠",可能是为了节省学校的带宽资源然后禁止学生注册教育邮箱账号.不过像一部电影所说的那样&qu ...

  6. Ribbon进行服务调用/负载均衡以及请求重试配置

    Ribbon负载均衡 经过对Eureka的认识,及Eureka集群的搭建,已经基本可以入门Eureka的使用.之前对于服务调用者我们是直接获取注册列表后通过 get(0) 的方式来获取第一个注册信息. ...

  7. MySQL之单表多表查询

    #1.单表查询 #单表查询语法 select <字段1,字段2....> from <表名> where <表达式> group by field 分组 havin ...

  8. 用vue开发一个公众号商城SPA——1.前期准备和写页面

    使用vue开发公众号商城 第1篇记录项目准备.搭建,写页面遇到第问题以及总结,持续更新 公司最近接了个商城项目,包括PC端商城.微信公众号网页商城.后台管理系统.这几天在做微信公众号商城,又新接触了很 ...

  9. 前端每日实战:31# 视频演示如何利用 CSS 的动画原理,创作一个乒乓球对打动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/rvgLzK 可交互视频教程 此视频 ...

  10. 续python学习(一)

    接上面没写完的知识点继写. 当然,这些知识点都很简单,可能没必要花费太多时间去记忆,多写写代码就会了. 5.字符串的使用.索引和切片是字符串主要的两个应用.索引:顾名思义就是找出某个字符在一个字符串中 ...