[英]Tic Tac Toe : Java
我真的迷路了,我在構建這個游戲時遇到了麻煩。 我需要你們的幫助。 一直在努力解決這個問題好幾天了。 我有三個班級,Driver、Board 和 Player。 我有司機,我認為董事會在控制之下。 我主要是在玩家類上掙扎。 這樣做的目的是讓電腦玩家能夠隨機輸入數組,然后讓玩家能夠輸入他們想要玩的地方。
public class Driver
{
public static void main(String[] args)
{
//new tic-tac-toe board
Board board = new Board();
//two new players (conputer and human)
Player computer = new Player(board, "X"); //Give computer player access to board and assign as X.
Player human = new Player(board, "O"); //Give human player access to board and assign as O.
board.print();
computer.computerMove();
//while the game is not over
while(!board.gameOver())
{
//let computer move first
computer.computerMove();
//print tic-tac-toe board
board.print();
//if the game is not over yet
if (!board.gameOver())
{
//let the human make a move
human.humanMove();
//if the game is over
if (board.gameOver())
{
//print the board
board.print();
}
}
}
//print out the winner (if there is one) of the game
board.printWinner();
}
}
板類
public class Board
{
private String player = "X";
private String cpu = "O";
int row = 3;
int column = 3;
private String[][] theBoard = new String[row][column] ;
public Board()
{
theBoard = theBoard;
}
public boolean gameOver()
{
if (theBoard[0][0] == player && theBoard[0][1] == player && theBoard[0][2] == player || // 1st row
theBoard[1][0] == player && theBoard[1][1] == player && theBoard[1][2] == player || // 2nd row
theBoard[2][0] == player && theBoard[2][1] == player && theBoard[2][2] == player || // 3rd row
theBoard[0][0] == player && theBoard[1][0] == player && theBoard[2][0] == player || // 1st col.
theBoard[0][1] == player && theBoard[1][1] == player && theBoard[2][1] == player || // 2nd col.
theBoard[0][2] == player && theBoard[1][2] == player && theBoard[2][2] == player || // 3rd col.
theBoard[0][0] == player && theBoard[1][1] == player && theBoard[2][2] == player || // Diagonal \
theBoard[2][0] == player && theBoard[1][1] == player && theBoard[0][2] == player) // Diagonal /
{
return false;
}
else if (theBoard[0][0] == cpu && theBoard[0][1] == cpu && theBoard[0][2] == cpu || // 1st row
theBoard[1][0] == cpu && theBoard[1][1] == cpu && theBoard[1][2] == cpu || // 2nd row
theBoard[2][0] == cpu && theBoard[2][1] == cpu && theBoard[2][2] == cpu || // 3rd row
theBoard[0][0] == cpu && theBoard[1][0] == cpu && theBoard[2][0] == cpu || // 1st col.
theBoard[0][1] == cpu && theBoard[1][1] == cpu && theBoard[2][1] == cpu || // 2nd col.
theBoard[0][2] == cpu && theBoard[1][2] == cpu && theBoard[2][2] == cpu || // 3rd col.
theBoard[0][0] == cpu && theBoard[1][1] == cpu && theBoard[2][2] == cpu || // Diagonal \
theBoard[2][0] == cpu && theBoard[1][1] == cpu && theBoard[0][2] == cpu) // Diagonal /
{
return false;
}
else{
return true;
}
}
public void print()
{
System.out.println(theBoard[0][0] + " | " + theBoard[0][1]+ " | " + theBoard[0][2] + "\n----------");
System.out.println(theBoard[1][0] + " | " + theBoard[1][1]+ " | " + theBoard[1][2] + "\n----------");
System.out.println(theBoard[2][0] + " | " + theBoard[2][1]+ " | " + theBoard[2][2] + "\n");
}
public void printWinner()
{
if (theBoard[0][0] == player && theBoard[0][1] == player && theBoard[0][2] == player || // 1st row
theBoard[1][0] == player && theBoard[1][1] == player && theBoard[1][2] == player || // 2nd row
theBoard[2][0] == player && theBoard[2][1] == player && theBoard[2][2] == player || // 3rd row
theBoard[0][0] == player && theBoard[1][0] == player && theBoard[2][0] == player || // 1st col.
theBoard[0][1] == player && theBoard[1][1] == player && theBoard[2][1] == player || // 2nd col.
theBoard[0][2] == player && theBoard[1][2] == player && theBoard[2][2] == player || // 3rd col.
theBoard[0][0] == player && theBoard[1][1] == player && theBoard[2][2] == player || // Diagonal \
theBoard[2][0] == player && theBoard[1][1] == player && theBoard[0][2] == player) // Diagonal /
{
System.out.println("X - won!");
}
else if (theBoard[0][0] == cpu && theBoard[0][1] == cpu && theBoard[0][2] == cpu || // 1st row
theBoard[1][0] == cpu && theBoard[1][1] == cpu && theBoard[1][2] == cpu || // 2nd row
theBoard[2][0] == cpu && theBoard[2][1] == cpu && theBoard[2][2] == cpu || // 3rd row
theBoard[0][0] == cpu && theBoard[1][0] == cpu && theBoard[2][0] == cpu || // 1st col.
theBoard[0][1] == cpu && theBoard[1][1] == cpu && theBoard[2][1] == cpu || // 2nd col.
theBoard[0][2] == cpu && theBoard[1][2] == cpu && theBoard[2][2] == cpu || // 3rd col.
theBoard[0][0] == cpu && theBoard[1][1] == cpu && theBoard[2][2] == cpu || // Diagonal \
theBoard[2][0] == cpu && theBoard[1][1] == cpu && theBoard[0][2] == cpu) // Diagonal /
{
System.out.println("O - won!");
}
}
}
而 Player 類,這是我最掙扎的一個。
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class Player
{
String player = "X";
String cpu = "O";
private Board ticTac;
public static Scanner scan = new Scanner(System.in);
public Player(Board board, String inBoard )
{
ticTac = board;
}
public void randomPlace()
{
for(int i = 0; i < 3; i ++)
{
for(int j = 0; j < 3; j++)
{
}
}
}
public void computerMove()
{
}
public void humanMove()
{
}
}
印刷
null | null | null
----------
null | null | null
----------
null | null | null
我想也許這可以幫助你。
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class Player
{
String player = "X";
String cpu = "O";
int row = 3;
int column = 3;
private Board ticTac;
public static Scanner scan = new Scanner(System.in);
public Player(Board board, String inBoard )
{
//here you have the board in player
tictac = board;
}
public void computerMove()
{ //here you can code something like this
tictac.put(tictac.getRandomFreePlace(),cpu);
}
public void humanMove(Position position)
{
tictac.put(position, human);
}
}
你必須在 Board 中編寫 put(Position, String) 和 getRandomFreePlace()
然后掃描玩家移動並打印板。
- - - - - - -更新 - - - - - - -
哦,你要初始化你的板子嗎? 你可以用 double for
for(i=0;i<row;i++){
for(j=0;j<column;j++){
//here you can set the value you want
theBoard[i][j]=0;
}
}
不知道我是否理解你的問題,你能重新表述一下或者舉個例子嗎?
據我了解,您希望在播放器類中使用您的棋盤方法是正確的嗎?
如果您創建了一個棋盤類並為其提供了所需的任何特征,那么在您的玩家類中,您可以創建一個新的“it”類,然后通過其名稱調用它以檢索您賦予它的任何特征。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.