统计信息
- 访问数:6136
- 博客数:141
- 建立时间:2008-01-05
- 更新时间:2008-05-09
P6Q27
2008-04-18 19:38:56
天气: 晴朗 心情: 高兴
/*filename:P6Q27.java
*Name:Wang Chaoran
*Description:
27 (Playing a TicTacToe game)
In a game of TicTacToe, two players take turns marking an available cell
in a 3 x 3 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 tokens and neither
player has achieved a win. Create a program for playing TicTacToe, as follows:
The program prompts the first player to enter an X token, and then prompts the
second player to enter an O token. Whenever a token is entered, the program refreshes
the board and determines the status of the game (win, draw, or unfinished).
To place a token, display two dialog boxes to prompt the user to enter the row and
the column for the token.
*/
import javax.swing.JOptionPane;
class P6Q27{
public static void main(String[] args){
String[][] display=new String[3][3];
int[][] board = new int[3][3];
for(int i=0;i<board.length;i++)
for(int j=0;j<board[i].length;j++){
board[i][j]=2;
display[i][j]="";
}
boolean status=true;
print(display);
int count=0;
while(status){
boolean repeat=true;
while(repeat){
String rowOne = JOptionPane.showInputDialog("PlayerOne:\nPlease enter row:");
String columnOne = JOptionPane.showInputDialog("PlayerOne:\nPlease enter column:");
int row = Integer.parseInt(rowOne);
int column = Integer.parseInt(columnOne);
if (board[column][row]==2){
board[column][row]=1;
display[column][row]="X";
print(display);
repeat=false;
}
else
System.out.println("This position has been occupied");
}
if(countOnRow(board)==1||countOnColumn(board)==1||countOnDiagnolLeft(board)==1||countOnDiagnolRight(board)==1){
System.out.println("Player One win");
status=false;
break;
}
count++;
if(count==9)
break;
repeat=true;
while(repeat){
String rowTwo = JOptionPane.showInputDialog("PlayerTwo:\nPlease enter row:");
String columnTwo = JOptionPane.showInputDialog("PlayerTwo:\nPlease enter column:");
int row=Integer.parseInt(rowTwo);
int column=Integer.parseInt(columnTwo);
if (board[column][row]==2){
board[column][row]=0;
display[column][row]="O";
print(display);
repeat=false;
}
else
System.out.println("This position has been occupied");
}
if(countOnRow(board)==0||countOnColumn(board)==0||countOnDiagnolLeft(board)==0||countOnDiagnolRight(board)==0){
System.out.println("Player Two wins");
status=false;
}
count++;
}
if(count==9)
System.out.println("Draw");
}
static int countOnRow(int[][] board){
for(int i=0;i<board.length;i++){
int j;
for(j=1;j<board[i].length&&board[i][0]==board[i][j];j++){
}
if(j==board[i].length)
return board[i][0];
}
return -1;
}
static int countOnColumn(int[][] board){
for(int i=0;i<board[0].length;i++){
int j;
for(j=1;j<board.length&&board[0][i]==board[j][i];j++){
}
if(j==board.length)
return board[0][i];
}
return -1;
}
static int countOnDiagnolLeft(int[][] board){
int i;
for(i=1;i<board.length&&board[0][0]==board[i][i];i++){
}
if(i==board.length)
return board[0][0];
return -1;
}
static int countOnDiagnolRight(int[][] board){
int j;
for(j=1;j<board.length&&board[0][board.length-1]==board[j][board.length-j-1];j++){
}
if(j==board.length)
return board[0][board.length-1];
return -1;
}
static void print(int[][] board){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++)
System.out.printf("%-5d",board[i][j]);
System.out.println();
}
System.out.println();
}
static void print(String[][] display){
System.out.printf("%-5s%-5s%-5s%-5s","","0","1","2");
System.out.println();
for(int i=0;i<display.length;i++){
System.out.printf("%-5d",i);
for(int j=0;j<display[i].length;j++){
System.out.printf("%-5s",display[i][j]);
}
System.out.println();
}
System.out.println();
}
}
导入论坛查看(26)回复(0)引用(0)好评(0) 差评(0)
加入收藏 编辑 审核TAG: computing