Facebook
From Sexy Panda, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 219
  1. import java.awt.*;
  2.  
  3. public class TwoPlayersGame extends Game{
  4.  
  5.     int[][] gameStatus;//0 - unset, 1 - player1, 2 - player2
  6.     int size;
  7.     int userScore;
  8.     Window window;
  9.     int movesCounter;
  10.     User player1;
  11.     User player2;
  12.     private static int whoseTurn = 0;
  13.  
  14.     TwoPlayersGame(int size, Window w) {
  15.         this.size = size;
  16.         window = w;
  17.         player1 = new User(window);
  18.         player2 = new User(window);
  19.         gameStatus = new int[size][size];
  20.         window = w;
  21.         userScore = 0;
  22.         movesCounter = 0;
  23.     }
  24.  
  25.     public void playTheGame() {
  26.  
  27.         player1.start();
  28.         player2.start();
  29.         while(isGameNotEnded()) {
  30.             synchronized(window) {
  31.                 while(whoseTurn%2 == 0) {
  32.                     try {
  33.                         player1.wait();
  34.                     } catch (InterruptedException e) {
  35.                         e.printStackTrace();
  36.                     }
  37.                 }
  38.                 player1Turn();//tutaj tak naprawdę ruch był już wykonany, teraz tylko zapisujemy gdzie się ruszono
  39.                 player1.run();//moveDone = false // stary poczekaj
  40.                 while(whoseTurn%2 != 0)
  41.                     if(isGameNotEnded()) {//tu trzeba analogicznie pozwolić spać jeszcze nie wiem komu, bo najpierw sobie pozwolę. Dobranoc. Lordzik zamknij japę.
  42.                         try {
  43.                             player2.wait();
  44.                             player2Turn();
  45.                         } catch (InterruptedException e) {
  46.                             e.printStackTrace();
  47.                         }
  48.                     }
  49.             }
  50.  
  51.         }
  52.         endGame();
  53.     }
  54.  
  55.     void player1Turn() {
  56.         Point p;
  57.         p = window.currentButton;
  58.         //do score Usera dodaje punkty z tury
  59.         gameStatus[p.x][p.y] = 1;
  60.         movesCounter += 1;
  61.         System.out.println("Teraz gra gracz1");
  62.     }
  63.  
  64.     void player2Turn() {
  65.         Point p;
  66.         p = window.currentButton;
  67. //                p = ai.play(gameStatus);
  68.         gameStatus[p.x][p.y] = 2;
  69.         //wpisz p do GUI
  70.         movesCounter += 1;
  71.         System.out.println("Teraz gra gracz2");
  72.     }
  73.  
  74.     public boolean checkIfScored() {
  75.         boolean scored = false;
  76.         for(int i = 0; i < size; i++) {
  77.             for(int j = 0; j < size; j++) {
  78. //                window.buttons//coś się wymyśli
  79.             }
  80.         }
  81.         return scored;
  82.     }
  83.  
  84.     private void endGame() {
  85.         //wyświetl punkty i zwycięzcę
  86.     }
  87.  
  88.     private boolean isGameNotEnded() {
  89.         return movesCounter < size * size;
  90.     }
  91.  
  92.     public static void setWhoseTurn(int whoseTurn) {
  93.         TwoPlayersGame.whoseTurn = whoseTurn;
  94.     }
  95.  
  96.     public static void addTurn() { TwoPlayersGame.whoseTurn += 1; }
  97. }