本文实例为大家分享了Java实现三子棋游戏的具体代码,供大家参考,具体内容如下
一、题目要求
编写程序,实现简单的三子棋游戏。在三子棋中,双方在3×3的棋盘中轮流下棋,一方用*示,另一方用O表示。如果一方的3个棋子占据了同一行,同一列或者对角线,则该方获胜。如果棋盘已被棋子占满,但没有一方获胜则出现平局。在程序中,一方为用户,用户在界面上输入每次下棋的位置;另一方下棋的位置为随机自动生成。
二、代码示例
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
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" + "平局,还差一点哦" ); } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/weixin_48419914/article/details/121068147