我正在尝试为“编程简介”课程的最终项目创建一个“Shadow TicTacToe”游戏。
该游戏应该起作用的方式是用户点击第一块板中的单元格并绘制X或O. X也会被绘制在同一个单元格中,但第二个游戏板上会有不同的位置。不同的点我的意思是,如果单元格(2,3)位于第一游戏板上的中心区块上,则相同的单元格将位于第二游戏板上的右上方区块(或在其他一些区块上)。
我创建了第一个和第二个板,每个板都在一个JPanel中。我正好无法为第一个游戏板上的一个单元格中的任何令牌创建一个方法,使其位于第二个游戏板上的同一个单元格中。我创建了2个单元格,每个游戏板一个,让我知道这是否只是复杂的事情或我真的需要2个单元格。我只是坚持项目的这一部分,任何帮助表示赞赏。
最终项目说明澄清:
代码(到目前为止,还有一些事情要做):
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.util.Random;
public class Almost extends JFrame{
// Initialzies cell array which will be the cells in grid 1
private Cell[][] cell = new Cell[3][3];
// Initalizes the cells array which will be the cells in grid 2
private Cells[][] cells = new Cells[3][3];
// Creates a boolean array
private boolean t[][] = new boolean[3][3];
// Intializes btnReset which will be a button that resets the game
private JButton btnReset;
//Initializes Random
Random rand = new Random();
// Initializes variables which will be used to create random ints
int f;
int g;
// Initializes JlblStatus
private JLabel jlblStatus = new JLabel(" ");
// Initializes the labels of the indexes for the cells on grid 1
private JLabel jlblIndex1 = new JLabel("(" + 0 + "," + 0 + ")");
private JLabel jlblIndex2 = new JLabel("(" + 0 + "," + 1 + ")");
private JLabel jlblIndex3 = new JLabel("(" + 0 + "," + 2 + ")");
private JLabel jlblIndex4 = new JLabel("(" + 1 + "," + 0 + ")");
private JLabel jlblIndex5 = new JLabel("(" + 1 + "," + 1 + ")");
private JLabel jlblIndex6 = new JLabel("(" + 1 + "," + 2 + ")");
private JLabel jlblIndex7 = new JLabel("(" + 2 + "," + 0 + ")");
private JLabel jlblIndex8 = new JLabel("(" + 2 + "," + 1 + ")");
private JLabel jlblIndex9 = new JLabel("(" + 2 + "," + 2 + ")");
// Initializes the labels of the indexes of the cells on grid 2
private JLabel jlblCells1 = new JLabel("(" + 0 + "," + 0 + ")");
private JLabel jlblCells2 = new JLabel("(" + 0 + "," + 1 + ")");
private JLabel jlblCells3 = new JLabel("(" + 0 + "," + 2 + ")");
private JLabel jlblCells4 = new JLabel("(" + 1 + "," + 0 + ")");
private JLabel jlblCells5 = new JLabel("(" + 1 + "," + 1 + ")");
private JLabel jlblCells6 = new JLabel("(" + 1 + "," + 2 + ")");
private JLabel jlblCells7 = new JLabel("(" + 2 + "," + 0 + ")");
private JLabel jlblCells8 = new JLabel("(" + 2 + "," + 1 + ")");
private JLabel jlblCells9 = new JLabel("(" + 2 + "," + 2 + ")");
private char currentPlayer = ' ';
// Method that builds the JFrame and JPanels
public Almost(){
// Title of the JFrame
JFrame frame = new JFrame("Shadow Tic Tac Toe Game");
// Makes the JFrame full screen
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// If x button is clicked than the JFrame closes
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initializes JPanel1
JPanel panel1 = new JPanel();
// Initializes JPanel2
JPanel panel2 = new JPanel();
// Adds panel1 which will hold the first TicTacToe game.
panel1.setLayout(new GridLayout(3,3,0,0));
for(int d = 0; d < 3; d++){
for(int c = 0; c < 3; c++){
panel1.add(cell[d][c] = new Cell());
// Sets size of the cells in JPanel1
cell[d][c].setPreferredSize(new Dimension(250,250));
}
}
// Visibly labels grid 1 cells with their cell index
cell[0][0].add(jlblIndex1);
cell[0][1].add(jlblIndex2);
cell[0][2].add(jlblIndex3);
cell[1][0].add(jlblIndex4);
cell[1][1].add(jlblIndex5);
cell[1][2].add(jlblIndex6);
cell[2][0].add(jlblIndex7);
cell[2][1].add(jlblIndex8);
cell[2][2].add(jlblIndex9);
// Puts the cells on grid 2
panel2.setLayout(new GridLayout(3,3,0,0));
int n = 0;
while(n < 9){
f=rand.nextInt(3);
g=rand.nextInt(3);
// If the index f & g are not already in boolean array t then teh while loop will run
// It will also place the cell index f & g on grid 2
while(t[f][g] == false){
t[f][g] = true;
panel2.add(cells[f][g] = new Cells());
cells[f][g].setPreferredSize(new Dimension(250,250));
n++;
}
}
// Visibly labels grid 2 cells with their cell index
cells[0][0].add(jlblCells1);
cells[0][1].add(jlblCells2);
cells[0][2].add(jlblCells3);
cells[1][0].add(jlblCells4);
cells[1][1].add(jlblCells5);
cells[1][2].add(jlblCells6);
cells[2][0].add(jlblCells7);
cells[2][1].add(jlblCells8);
cells[2][2].add(jlblCells9);
// Adds Panel1 to the JFrame
frame.add(panel1, BorderLayout.WEST);
// Adds Panel2 to the JFrame
frame.add(panel2, BorderLayout.EAST);
// Updates the status of the game here (win/lose/draw/whose turn it is
frame.add(jlblStatus, BorderLayout.SOUTH);
// Sets size of the message area at the bottom of the frame
jlblStatus.setPreferredSize(new Dimension(100,100));
// Adds a Reset button to the JFrame
buildReset();
// Puts the reset button on the top of the JFrame
frame.add(btnReset, BorderLayout.NORTH);
// Shows the Instructions of the game
Instructions();
// Calls method Chose() which allows the player to chose which token they will play as
Chose();
frame.pack();
// Sets it so the JFrame is visible to the user
frame.setVisible(true);
}
// Method that creates the Instructions for the game. Will be shown to the user prior to the user picking his token
public void Instructions(){
JOptionPane.showMessageDialog(null,"INSTRUCTIONS" + "This game is called a 'Shadow Tic Tac Toe Game'. In this game there will be two Tic Tac Toe game boards, though only one is playable. \nBut you can win on either board. Lets say you place your token on the center tile at cell(2,3). \nAn X will be drawn on that spot on board 1 and on a randomized spot on the second game board at cell(2,3). \nYou will be able to see the cell indexes before you click on a cell so you can form a strategy");
}
// Method that lets the user chose his/her token
public void Chose(){
int f = 2;
// While f == 2 the loop will run
while(f == 2){
String input = JOptionPane.showInputDialog("CHOSE" + "\nPlease select your token. \nPress 1 for X and 2 for O.");
// Reads in the user input. Input put into a variable called pawn
int pawn = Integer.parseInt(input);
// If user input 1 his/her token will be X. F will equal 3 so the loop does not run again
if(input.equals("1")){
currentPlayer = 'X';
f = 3;
// If user input 2 his/her token will be O. F will equal 3 so the loop does not run again
}else if(input.equals("2")){
currentPlayer = 'O';
f = 3;
// If user does not enter in either a 1 or 2 an error message will appear. f wil equal 2 so the loop runs again and asks the user to chose his/her token again
}else{
JOptionPane.showMessageDialog(null,"ERROR INVALID RESPONSE");
f = 2;
}
}
}
// Builds the button btnReset
public void buildReset(){
btnReset = new JButton("Reset");
btnReset.addActionListener(new BtnListener());
}
// If btnReset if clicked it resets the Almost() method and starts the game over
private class BtnListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Almost b = new Almost();
}
}
// The class for the cells on grid 2
// Responsible for setting the tokens for grid 2 and drawing the tokens in the cells in grid 2
public class Cells extends JPanel{
// Initalizes tok and initailly sets it to nothing
private char tok = ' ';
public Cells(){
// Sets the border for the cells to the color black
setBorder(new LineBorder(Color.black,1));
}
// Sets tok to char d, which will be currentPlayer
public void setTok(char d){
tok = d;
repaint();
}
// Responsible for getting the tokens in cells in grid 2. Mainly used in Full2() and Won2()
public char getTok(){
return tok;
}
// Paints the token in the cells in grid 2
protected void paintComponent(Graphics g){
super.paintComponent(g);
// If tok equals X it will draw an X
if(tok == 'X'){
g.drawLine(10,10,getWidth() - 10, getHeight()-10);
g.drawLine(getWidth()-10,10,10,getHeight()-10);
// If tok equals O an oval will be drawn
}else if (tok == 'O'){
g.drawOval(10,10,getWidth()-20, getHeight()-20);
}
}
}
// Class for the cells in grid 1
public class Cell extends JPanel{
private char token = ' ';
String Id ="";
//write getter setter for it and do same code as you have done for to
public void setToken(char c){
token = c;
repaint();
}
// When called upon it gets the token in the cells specified and return them
// Called in Full1() and Won1()
public char getToken(){
return token;
}
public Cell(){
// Sets the border for the cells to the color black
setBorder(new LineBorder(Color.black, 1));
addMouseListener(new MyMouseListener());
}
// Paints the token in the cell on grid 1 that was clicked
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// If token equals X it paints an X
if (token == 'X') {
g.drawLine(10,10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10,10,10, getHeight() - 10);
}
// If token equals O it will draw an oval
else if (token == 'O') {
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
}
private class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
// If cell is empty and game is not over
if (token == ' ' && currentPlayer != ' ') {
// Sets the token in the cell that was clicked in grid 1
setToken(currentPlayer);
// Sets the token in the corresponding cell in grid 2
PlaceGrid2();
// Win Solutions
if (Won1(currentPlayer)){
jlblStatus.setText(currentPlayer + " wins on board1!");
Change();
}else if (Won2(currentPlayer)){
jlblStatus.setText(" Player X wins on board 2!");
}else{
Change();
}
}
}
}
}
// Changes the players turn
public void Change(){
if(currentPlayer == 'X'){
currentPlayer = 'O';
}else{
currentPlayer = 'X';
}
jlblStatus.setText("It is " + currentPlayer + " 's turn");
}
// Places token on grid 2
public void PlaceGrid2(){
for(int c = 0; c < 3; c++){
for(int d = 0; d < 3; d++){
if(cell[c][d].getToken() == 'X'){
cells[c][d].setTok('X');
}
if(cell[c][d].getToken() == 'O'){
cells[c][d].setTok('O');
}
}
}
}
// Checks to see if grid 1 is full
public boolean Full1(){
for(int d = 0; d < 3; d++)
for(int c = 0; c < 3; c++)
if(cell[d][c].getToken() == ' ')
return false;
return true;
}
// Checks to see if grid 2 is full
public boolean Full2(){
for(int d = 0; d < 3; d++)
for(int c = 0; c < 3; c++)
if(cells[d][c].getTok() == ' ')
return false;
return true;
}
// Checks to see if anyone won on grid 1
public boolean Won1(char token){
for(int d = 0; d < 3; d++){
if(cell[d][0].getToken() == token && cell[d][1].getToken() == token && cell[d][2].getToken() == token){
return true;
}
}
for(int c = 0; c < 3; c++){
if(cell[0][c].getToken() == token && cell[1][c].getToken() == token && cell[2][c].getToken() == token){
return true;
}
}
if(cell[0][0].getToken() == token && cell[1][1].getToken() == token && cell[2][2].getToken() == token){
return true;
}
if(cell[0][2].getToken() == token && cell[1][1].getToken() == token && cell[2][0].getToken() == token){
return true;
}
return false;
}
// Checks to see if anyone won on grid 2
public boolean Won2(char tok){
for(int d = 0; d < 3; d++){
if(cells[d][0].getTok() == tok && cells[d][1].getTok() == tok && cells[d][2].getTok() == tok){
return true;
}
}
for(int c = 0; c < 3; c++){
if(cells[0][c].getTok() == tok && cells[1][c].getTok() == tok && cells[2][c].getTok() == tok){
return true;
}
}
if(cells[0][0].getTok() == tok && cells[1][1].getTok() == tok && cells[2][2].getTok() == tok){
return true;
}
if(cells[0][2].getTok() == tok && cells[1][1].getTok() == tok && cells[2][0].getTok() == tok){
return true;
}
return false;
}
// Main method
public static void main(String [] args){
new Almost();
}
}
您需要为每个Cell添加一个索引,该索引将映射来自板1和板2的单元。您将在第二个板中绘制与第一个板上具有相同索引的Cell的令牌。正如您在图像中看到的那样。因为如果你通过坐标系统,你会松开轨道并且板子将是平等的。