繁体   English   中英

游戏线程问题

[英]Issues with threads in game

我一直在尝试使用一些基本的多线程来制作一个基本的井字游戏,但不知何故,两个线程都可以访问同步语句,这让我很困惑。

这是代码:

import java.util.*;
public class TicTacToe extends Thread {
static  char[][] map = new char[3][3];
static char notsCrosses;
static volatile boolean gameFinish = false;


public TicTacToe(String n){
    super(n);
}
public TicTacToe(){

}
public synchronized String pickPosition(int x){

    switch (x){
        case 1: map[0][0] = notsCrosses; return "00";
        case 2: map[0][1] = notsCrosses; return "01";
        case 3: map[0][2] = notsCrosses; return "02";
        case 4: map[1][0] = notsCrosses; return "10";
        case 5: map[1][1] = notsCrosses; return "11";
        case 6: map[1][2] = notsCrosses; return "12";
        case 7: map[2][0] = notsCrosses; return "20";
        case 8: map[2][1] = notsCrosses; return "21";
        case 9: map[2][2] = notsCrosses; return "22";
    }
    return null;
}

public void run() {
    if (this.getName() == "player1") {
        notsCrosses = 'X';
    } else {
        notsCrosses = 'O';
    }
    Scanner input = new Scanner(System.in);
    synchronized (this) {
        while (!gameFinish) {
            try {
                System.out.print(this.getName() + " Enter the position (1-9): ");
                int position = input.nextInt();
                pickPosition(position);

                System.out.print("h");
                printMaze();
                int yCood = Integer.parseInt(pickPosition(position).substring(0, 1));
                int xCood = Integer.parseInt(pickPosition(position).substring(1, 2));

                if (hasWon(map, xCood, yCood)) {
                    System.out.println("You win! " + this.getName());
                    gameFinish = true;
                }

                notify();
                wait();
            } catch (InterruptedException i) {

            }
        }
    }
}


public static void main(String[] args) {
    for(int i=0; i<map.length; i++){
        for(int j=0; j<map[i].length; j++){
            map[i][j] = '-';
        }

    }
    new TicTacToe().printMaze();

    TicTacToe player1 = new TicTacToe("player1");
    TicTacToe player2 = new TicTacToe("player2");

    player1.start();
    player2.start();

}
public synchronized void printMaze(){
        for(int i=0; i<map.length; i++){
            if(i==0) {
                for (int x = 0; x < 3; x++) {
                    System.out.print("---");
                }
                System.out.println();
            }
            for(int j=0; j<map[i].length; j++) {
                System.out.print("|");
                System.out.print(map[i][j]);
                System.out.print("|");
            }
            System.out.println();
            for(int x=0; x<3; x++) {
                System.out.print("---");
            }
            System.out.println();

        }

}


public synchronized boolean hasWon(char[][] m,int xCood, int yCood){
    if(checkRow(m,yCood) || checkColumn(m,xCood) || checkDiagonal(m,xCood,yCood)){
        return true;
    }
    return false;


}

public synchronized boolean checkRow(char[][] m,int yCood){
    for(int i=0; i<3; i++){
        if(map[yCood][i] != notsCrosses){
            return false;
        }
    }
    return true;
}

public synchronized boolean checkColumn(char[][] m,int xCood){
    for(int i=0; i<3; i++){
        if(map[i][xCood] != notsCrosses){
            return false;
        }
    }
    return true;
}
public synchronized boolean checkDiagonal(char[][] m,int xCood, int yCood){
    if(map[0][0]==notsCrosses && map[1][1]==notsCrosses && map[2][2]==notsCrosses){
            return true;
    }
    else if(map[2][0]==notsCrosses && map[1][1]==notsCrosses && map[0][2]==notsCrosses){
            return true;
    }
    return false;


}
}

我在运行方法中使用同步语句,我想要做的是只允许一个线程(玩家)一次访问该代码并检查易失性变量“gameFinish”。

你有两个错误:

  1. 您有两个播放器,每个播放器都自行同步。 那是行不通的。 您需要同步一些共享的单一事物。

  2. 您的代码无处跟踪轮到哪个玩家。 所以你的代码不知道什么时候停止等待。 它只是猜测并且可能猜测错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM