Java实现三子棋游戏
时间:2022-12-26 10:53:05|栏目:JAVA代码|点击: 次
本文实例为大家分享了Java实现三子棋游戏的具体代码,供大家参考,具体内容如下
一、题目要求
编写程序,实现简单的三子棋游戏。在三子棋中,双方在3×3的棋盘中轮流下棋,一方用*示,另一方用O表示。如果一方的3个棋子占据了同一行,同一列或者对角线,则该方获胜。如果棋盘已被棋子占满,但没有一方获胜则出现平局。在程序中,一方为用户,用户在界面上输入每次下棋的位置;另一方下棋的位置为随机自动生成。
二、代码示例
import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { char []s=new char[9]; for(int i=0;i<9;i++) { s[i]=(char)(i+49); } int i=0; while(i++<5) { String str=JOptionPane.showInputDialog("------------------\n" +"| "+s[0]+" | "+s[1]+" | "+s[2]+" |\n" +"| "+s[3]+" | "+s[4]+" | "+s[5]+" |\n" +"| "+s[6]+" | "+s[7]+" | "+s[8]+" |\n" +"------------------\n" + "请输入位置:"); while(str.length()==0||(str.charAt(0)-49)<0||(str.charAt(0)-49)>8) { str=JOptionPane.showInputDialog("------------------\n" +"| "+s[0]+" | "+s[1]+" | "+s[2]+" |\n" +"| "+s[3]+" | "+s[4]+" | "+s[5]+" |\n" +"| "+s[6]+" | "+s[7]+" | "+s[8]+" |\n" +"------------------\n" + "输入错误,请重新输入:"); } char index=str.charAt(0); while(s[index-49]=='*'||s[index-49]=='o') { str=JOptionPane.showInputDialog("------------------\n" +"| "+s[0]+" | "+s[1]+" | "+s[2]+" |\n" +"| "+s[3]+" | "+s[4]+" | "+s[5]+" |\n" +"| "+s[6]+" | "+s[7]+" | "+s[8]+" |\n" +"------------------\n" + "此位置已占用,请重新输入:"); index=str.charAt(0); while(str.length()==0||(str.charAt(0)-49)<0||(str.charAt(0)-49)>8) { str=JOptionPane.showInputDialog("------------------\n" +"| "+s[0]+" | "+s[1]+" | "+s[2]+" |\n" +"| "+s[3]+" | "+s[4]+" | "+s[5]+" |\n" +"| "+s[6]+" | "+s[7]+" | "+s[8]+" |\n" +"------------------\n" + "输入错误,请重新输入:"); } } s[index-49]='*'; if((s[0]=='*'&&s[3]=='*'&&s[6]=='*')|| (s[1]=='*'&&s[4]=='*'&&s[7]=='*')|| (s[2]=='*'&&s[5]=='*'&&s[8]=='*')|| (s[0]=='*'&&s[1]=='*'&&s[2]=='*')|| (s[3]=='*'&&s[4]=='*'&&s[5]=='*')|| (s[6]=='*'&&s[7]=='*'&&s[8]=='*')|| (s[0]=='*'&&s[4]=='*'&&s[8]=='*')|| (s[2]=='*'&&s[4]=='*'&&s[6]=='*')) { JOptionPane.showMessageDialog(null, "------------------\n" +"| "+s[0]+" | "+s[1]+" | "+s[2]+" |\n" +"| "+s[3]+" | "+s[4]+" | "+s[5]+" |\n" +"| "+s[6]+" | "+s[7]+" | "+s[8]+" |\n" +"------------------\n" + "恭喜你赢了"); break; } int b=(int)(Math.random()*9); while(s[b]=='*'||s[b]=='o') { b=(int)(Math.random()*9); } s[b]='o'; if((s[0]=='o'&&s[3]=='o'&&s[6]=='o')|| (s[1]=='o'&&s[4]=='o'&&s[7]=='o')|| (s[2]=='o'&&s[5]=='o'&&s[8]=='o')|| (s[0]=='o'&&s[1]=='o'&&s[2]=='o')|| (s[3]=='o'&&s[4]=='o'&&s[5]=='o')|| (s[6]=='o'&&s[7]=='o'&&s[8]=='o')|| (s[0]=='o'&&s[4]=='o'&&s[8]=='o')|| (s[2]=='o'&&s[4]=='o'&&s[6]=='o')) { JOptionPane.showMessageDialog(null, "------------------\n" +"| "+s[0]+" | "+s[1]+" | "+s[2]+" |\n" +"| "+s[3]+" | "+s[4]+" | "+s[5]+" |\n" +"| "+s[6]+" | "+s[7]+" | "+s[8]+" |\n" +"------------------\n" + "失败了,再接再励"); break; } } if(i==6) { JOptionPane.showMessageDialog(null, "------------------\n" +"| "+s[0]+" | "+s[1]+" | "+s[2]+" |\n" +"| "+s[3]+" | "+s[4]+" | "+s[5]+" |\n" +"| "+s[6]+" | "+s[7]+" | "+s[8]+" |\n" +"------------------\n" + "平局,还差一点哦"); } } }
上一篇:SpringBoot整合Xxl-Job的完整步骤记录
栏 目:JAVA代码
下一篇:MyBatis启动时控制台无限输出日志的原因及解决办法
本文标题:Java实现三子棋游戏
本文地址:http://www.codeinn.net/misctech/222236.html