java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart!
这是一个井字棋游戏的java实现。摘录于stackoverflow。
游戏规则很简单,只要一方棋子在水平线,垂直线或者对角线任意一条线上排列成功即为获胜。
作者原先的代码存在着一些问题:
代码如下:
一共有几个类: play, player, human, computer, set
Play类:
import java.util.Scanner;
public class play {
public static void main(String[] args) {
System.out.println("Welcome to Tickle Tackle Toe!!! :D");
System.out.println();
//creat markers
String marker1 = "x";
String marker2 = "o";
boolean playAgain = true;
Scanner s = new Scanner(System.in);
//create player objects
Human human = new Human();
Computer computer = new Computer();
while(playAgain){
//run board setup
set Setup = new set();
Setup.createBoard();
Setup.printBoard();
System.out.println("please choose your marker");
System.out.println("type 1 for 'x' or 2 for 'o'");
//set markers
if(s.nextInt() == 1){
// create player objects
human.setMarker("x");
computer.setMarker("o");
}
else
{
human.setMarker("o");
computer.setMarker("x");
}
// determine who goes first
int first = (int) (Math.random() * 2);
boolean won = false;
int turns = 0;
if(first == 0){
System.out.println("You go first!");
System.out.println();
while(!won){
human.takeTurn(Setup.getBoard());
turns++;
Setup.printBoard();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("Congrats you won!");
}
if(turns == 9){
won = true;
System.out.println("Its a draw!");
break;
}
if(!won){
computer.takeTurn(Setup.getBoard(), human);
turns++;
System.out.println();
Setup.printBoard();
System.out.println();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("You lost!");
}
if(turns == 9){
won = true;
System.out.println("Its a draw!");
break;
}
}
} // close while 1
}
else {
System.out.println("Computer goes first!");
System.out.println();
while(!won){
computer.takeTurn(Setup.getBoard(), human);
turns++;
Setup.printBoard();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("You lost!");
}
if(!won){
human.takeTurn(Setup.getBoard());
turns++;
System.out.println();
Setup.printBoard();
System.out.println();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("Congrats you won!");
}
}
} // close while 2
}
System.out.println("Would you like to play again? Type 1 for yes or 2 to quit");
System.out.println();
if(s.nextInt() == 2){
playAgain = false;
}
}
}
}
Player类
public class player {
public String Marker;
// set marker method
public void setMarker(String marker){
Marker = marker;
}
//get marker method
public String getMarker(){
return Marker;
}
}
棋局Set类:
import java.util.Random;
import java.util.Scanner;
public class set {
// setup variables default board size and board
private int N = 3;
public String[][] board = new String [N][N];
public boolean hasWon (String [] [] board){
//horizontal
for(int i = 0; i<3; i++){
if(board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2])){
return true;
}
}
//vertical
for(int i = 0; i<3; i++){
if(board [0][i].equals(board[1][i]) && board[1][i].equals(board[2][i])){
return true;
}
}
//diagonal
if(board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2]) || board[2][0].equals(board[1][1]) && board[1][1].equals(board[0][2]))
return true;
return false;
}
int x = 1;
public void createBoard(){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board.length; j++){
board[i][j] = "" + (x);
x++;
}
}
}
public void printBoard(){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board.length; j++){
System.out.print("[" + board[i][j] + "]" + " ");
}
System.out.println();
}
}
public String[][] getBoard(){
return board;
}
}
Computer类
class Computer extends player {
public Computer(){
}
int boardsize = 3;
public void takeTurn(String[][] board, Human human) {
int vertical = 0;
int horizontal = 0;
int diagonal = 0;
boolean mademove = false;
// check if you can take a win horizontally
for(int i = 0; i<3; i++){
if(board[0][i].equals(board[1][i]) && board[0][i].equals(Marker)){
if(board[2][i] != human.getMarker()){
board[2][i] = Marker;
mademove = true;
return;
}
}
}
for(int i = 0; i<3; i++){
if(board[2][i].equals(board[1][i]) && board[2][i].equals(Marker)){
if(board[0][i] != human.getMarker()){
board[0][i] = Marker;
mademove = true;
return;
}
}
}
// check if you can take a win vertically
for(int i = 0; i<3; i++){
if(board[i][0].equals(board[i][1]) && board[i][0].equals(Marker)){
if(board[i][2] != human.getMarker()){
board[i][2] = Marker;
mademove = true;
return;
}
}
}
for(int i = 0; i<3; i++){
if(board[i][2].equals(board[i][1]) && board[i][2].equals(Marker)){
if(board[i][0] != human.getMarker()){
board[i][0] = Marker;
mademove = true;
return;
}
}
}
// check if you can take a win diagonally
if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){
if(board[2][2] != human.getMarker()){
board[2][2] = Marker;
mademove = true;
return;
}
}
if(board[2][2].equals(board[1][1]) && board[2][2].equals(Marker)){
if(board[0][0] != human.getMarker()){
board[0][0] = Marker;
mademove = true;
return;
}
}
if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){
if(board[2][2] != human.getMarker()){
board[2][2] = Marker;
mademove = true;
return;
}
}
if(board[0][2].equals(board[1][1]) && board[0][2].equals(Marker)){
if(board[2][0] != human.getMarker()){
board[2][0] = Marker;
mademove = true;
return;
}
}
if(board[2][0].equals(board[1][1]) && board[2][0].equals(Marker)){
if(board[0][2] != human.getMarker()){
board[0][2] = Marker;
mademove = true;
return;
}
}
// BLOCKS!!!! //
// check if you can block a win horizontally
for(int i = 0; i<3; i++){
if(board[0][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){
if(board[2][i] != Marker){
board[2][i] = Marker;
mademove = true;
return;
}
}
}
for(int i = 0; i<3; i++){
if(board[2][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){
if(board[0][i] != Marker){
board[0][i] = Marker;
mademove = true;
return;
}
}
}
// check if you can block a win horizontally
for(int i = 0; i<3; i++){
if(board[i][0].equals(board[i][1]) && board[i][0].equals(human.getMarker())){
if(board[i][2] != Marker){
board[i][2] = Marker;
mademove = true;
return;
}
}
}
for(int i = 0; i<3; i++){
if(board[i][2].equals(board[i][1]) && board[i][2].equals(human.getMarker())){
if(board[i][0] != Marker){
board[i][0] = Marker;
mademove = true;
return;
}
}
}
// check if you can block a win diagonally
if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){
if(board[2][2] != Marker){
board[2][2] = Marker;
mademove = true;
return;
}
}
if(board[2][2].equals(board[1][1]) && board[2][2].equals(human.getMarker())){
if(board[0][0] != Marker){
board[0][0] = Marker;
mademove = true;
return;
}
}
if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){
board[2][2] = Marker;
mademove = true;
return;
}
if(board[0][2].equals(board[1][1]) && board[0][2].equals(human.getMarker())){
if(board[2][0] != Marker){
board[2][0] = Marker;
mademove = true;
return;
}
}
if(board[2][0].equals(board[1][1]) && board[2][0].equals(human.getMarker())){
if(board[0][2] != Marker){
board[0][2] = Marker;
mademove = true;
return;
}
}
// make random move if above rules dont apply
for(int i = 0; i<3; i++){
if(board[i][0] != "x" && board[i][0] != "o"){
board[i][0] = Marker;
return;
}
}
for(int i = 0; i<3; i++){
if(board[i][1] != "x" && board[i][0] != "o"){
board[i][1] = Marker;
return;
}
}
for(int i = 0; i<3; i++){
if(board[i][2] != "x" && board[i][0] != "o"){
board[i][2] = Marker;
return;
}
}
}
}
Human类
import java.util.Scanner;
class Human extends player {
public Human(){
}
public void takeTurn(String[][] board) {
Scanner s = new Scanner(System.in);
boolean turn = true;
while(turn){
System.out.println("please enter row");
int row = s.nextInt();
System.out.println("please enter col");
int col = s.nextInt();
System.out.print("you entered "+row+" "+col);
System.out.println();
if(board[row - 1][col - 1] != "x" && board[row - 1][col - 1] != "o"){
board[row - 1][col - 1] = Marker;
turn = false;
} // closes if
else {
System.out.println("Already Marker here! please guess again!");
}
} // ends while
} // ends method
} // ends class
这段代码需要进一步调试。摘录此仅供参考。
原文出处:https://stackoverflow.com/questions/10645381/tictactoe-ai-java
java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart!相关推荐
- 带栩字的优美古诗句_带栩字的名字_以栩字起名-尚名网
带栩字的名字_以栩字起名-尚名网 一个含义吉祥.美好.典雅的名字,某种程度上可以激励自己奋发努力.因此,我们在给宝宝起名的时候,一定要注意选择那些字义和寓意都很美好的字眼. 栩字五行属木 五行拼音繁体 ...
- JAVA程序中怎么看线程的个数_一个文件中有10000个数,用Java实现一个多线程程序将这...
18 推荐 运行结果: 编辑于 2015-07-16 17:20:57 回复(11) 12 自己重写了一下推荐答案,加了些注释方便理解 package threadpackage; import ja ...
- java写病毒程序代码_一个用JAVA写的清除EXE病毒文件的程序(转)
Clear.java 这是一个主类,主要是负责运行程序和参数检查,不是核心 程序代码: import java.io.*; public class Clear{ public static void ...
- java 多态判断非空_跳槽涨薪季面试题之java基础(一)
点击上方[全栈开发者社区]→右上角[...]→[设为星标⭐] 为迎接金九银十跳槽涨薪季,小编汇总了java精编版面试题,大概从java基础.java8特性.多线程.spring.springboot. ...
- java.线程池 线程数_如何在线程“ main”中修复异常java.lang.NoClassDefFoundError:Java中的org / slf4j / LoggerFactory...
java.线程池 线程数 此错误表示您的代码或您在应用程序中使用的任何外部库都在使用SLF4J库 (一个开放源代码日志记录库),但无法找到所需的JAR文件,例如slf4j-api-1.7.2.jar因 ...
- 新手学java还是python知乎_编程初学者应该先学C++、Java还是Python?
最近,看到这样的一个话题:"打算自学编程,但是不知道该先学哪门语言入门?编程初学者应该先学C++.Java还是Python?",作为一个新手,应该学什么语言入门比较好呢?相信这是困 ...
- java二级考试真题_计算机等级考试真题2(JAVA)
1. D (A)类属于JAVA语言的引用数据类型. (B)接口属于JAVA语言的引用数据类型. (C)数组属于JAVA语言的引用数据类型. (D)double不属于JAVA语言的引用数据类型. 2. ...
- java 读取excel2007 内存不足_内存不足错误 – 写入Excel时的Java堆空间
我有近100,000条记录的数据,我正在尝试使用XSSFWorkbook通过 Java代码将数据写入.xlsx文件.我能够将数据库中的所有数据提取到ArrayList.通过迭代ArryList,我将数 ...
- 编程一个最简单游戏_一个关于AI编程的游戏
点击上方"机器学习与统计学",选择"置顶"公众号 重磅干货,第一时间送达 周末推荐一个正在玩的游戏,挺好玩的. <异常>是一个关于AI编程的游戏,在 ...
最新文章
- C#验证Email是否真正存在,不是验证邮件格式,是邮件地址是否存在 .
- Linux 修改yum 源
- 通过jconsole监控tomcat JVM 内存、线程、CPU
- java field 获得值_反射通用获取字段值
- Mysql导入excel数据,解决某些特殊字符乱码问题
- JDK源码 - BitSet的实现
- HTML高仿哔哩哔哩(B站)视频网站整站模板
- Scala 深入浅出实战经典 第81讲:Scala中List的构造是的类型约束逆变、协变、下界详解...
- 直接插入排序及优化(二分查找插入排序)
- 标准评分卡分数计算原理_学习评分卡Gini指标?这篇看完就够了!
- 人脸识别示例代码解析(二)——人脸识别解析
- 使用pdfobject.js实现在线浏览PDF--后台上传保存文件
- 和cc2500通信时总是读入0F
- journalctl用法详解
- linux 更改sh文件权限不够,linux权限不够,sh不能用
- 《三国空城计》何为真知己真智慧
- whois域名查询工具在线使用
- 智慧零售产业应用实战,30分钟上手的高精度商品识别
- ad19怎么手动布线_AD19如何使用强大的自动布线功能
- 读书笔记_《统计陷阱》达莱尔.哈夫
热门文章
- pppoe拨号的外网ip无法ping通_【思唯网络学院】 五大网络概念:IP地址、子网掩码、网关、DHCP服务和PPPoE拨号...
- mysql主键设置after_mysql如何改变主键属性
- matlab多元函数_函数的计算机处理8(1)_1MATLAB
- django2.1支持的mysql版本_一文解决django 2.2与mysql兼容性问题
- java 过滤脚本_【快学SpringBoot】过滤XSS脚本攻击(包括json格式)
- java软件工程_java复习
- ios 高德获取定位_解决ios11不支持高德地图API定位功能的方法
- python pdf转word 表格_太赞了!Pdf转Word,我用Python 轻松搞定表格和水印!
- css居中的几种方法_CSS几种常用的水平垂直居中对齐方法
- 华为路由器A1如何设置虚拟服务器,华为路由器A1如何设置虚拟服务器