JAVA编程训练——Game: playing a TicTacToe game
来源:互联网 发布:儿童英语网络课程 编辑:程序博客网 时间:2024/05/21 07:49
In a game of TicTacToe, two players take turns marking an available cell in a 3x3 grid with their respective tokens(either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won, A draw (no winner) occurs when all the cells on the grid have been filled with token and neither player has achieved a win. Create a program for playing TicTacToe.
The program prompts two players to enter X token and O token alternately. Whenever a token is entered, the program redisplays the broad on the console and determines the status of the game (win, draw, or continue).
import java.util.Date;import java.util.Scanner;import java.util.Calendar;import javax.swing.JFrame;public class TicIacIoe{char[][] block={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};TicIacIoe(){}public void show(){System.out.println("-------");for (int i = 0; i < 3; ++i){for (int j = 0; j < 3; ++j)System.out.print("|" + block[i][j]);System.out.println("|");System.out.println("-------");}}public void playx(){int i=0,j=0;Scanner input = new Scanner(System.in);while(true){System.out.print("Enter a row (0, 1, or 2) for player X:"); i=input.nextInt(); System.out.print("Enter a column (0, 1, or 2) for player X:"); j=input.nextInt(); if(i>2||j>2) System.out.println("Your range is wrong!"); else if(block[i][j]!=' ') System.out.println("The step is wrong!"); else break;}block[i][j]='X';}public void playo(){int i=0,j=0;Scanner input = new Scanner(System.in);while(true){System.out.print("Enter a row (0, 1, or 2) for player O:"); i=input.nextInt(); System.out.print("Enter a column (0, 1, or 2) for player O:"); j=input.nextInt(); if(block[i][j]!=' '||i>2||j>2) System.out.println("The step is wrong!"); else break;}block[i][j]='O';}public boolean check(char n){if((block[0][0]==n&&block[0][1]==n&&block[0][2]==n)||(block[1][0]==n&&block[1][1]==n&&block[1][2]==n)||(block[2][0]==n&&block[2][1]==n&&block[2][2]==n))return true;if((block[0][0]==n&&block[1][0]==n&&block[2][0]==n)||(block[0][1]==n&&block[1][1]==n&&block[2][1]==n)||(block[0][2]==n&&block[1][2]==n&&block[2][2]==n)) return true;if((block[0][0]==n&&block[1][1]==n&&block[2][2]==n)||(block[0][2]==n&&block[1][1]==n&&block[2][0]==n)) return true;return false;}public static void main(String[] args){TicIacIoe ticIacIoe =new TicIacIoe();int cnt=0;while(true){ticIacIoe.show();ticIacIoe.playx();ticIacIoe.show();if(ticIacIoe.check('X')){System.out.println("X palyer won");break;}cnt++;if(cnt==9){System.out.println("It is a draw");break;}ticIacIoe.playo();ticIacIoe.show();if(ticIacIoe.check('O')){System.out.println("O palyer won");break;}cnt++;}}}
0 0
- JAVA编程训练——Game: playing a TicTacToe game
- JAVA Playing a TicTacToe game
- JAVA三星题之playing a TicTacToe game
- JAVA编程训练——Game:scissor, rock, paper
- JAVA编程训练——Game: connect four
- Project: TicTacToe Game
- A game——hihoCoder173
- Develop one game for playing
- usaco A Game Java dp
- JAVA Game: picking a card
- cf——A. The Great Game
- 北大ACM2484——A Funny Game
- hdu 3094——A tree game
- 博弈——Play a game
- hdu1564—Play a game(博弈论)
- A Game
- A Game
- A. Game
- C# 多线程学习系列二:认识Thread类及其方法
- git通过gitosis来管理权限(三)
- 2015河南科技学院高校CSDN俱乐部--电脑选购常识
- java RSA加密解密
- 判断当前网络
- JAVA编程训练——Game: playing a TicTacToe game
- jQuery选择器
- 【详解】Python统一解密
- 最小公倍数简洁代码
- -(void)viewWillAppear:(BOOL)animated,- (void)viewDidLoad 的区别
- 【FAQ】如何创建ubuntu sudo用户?
- Leetcode || Merge Two Sorted Lists
- IOS开发技巧--模拟器 cannot run 项目.
- js、php、thrift的坑