Advertisement
Talar97

[GK] Processing - kółko i krzyżyk

May 18th, 2018
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. Game game;
  2.  
  3. void setup(){
  4.   int activePlayer = 2;
  5.   int status = 0;
  6.  
  7.   //  GameMode:
  8.   //  0 - CPU vs Player
  9.   //  1 - Player vs Player
  10.   int gameMode = 0;
  11.  
  12.   size(500, 600);
  13.   background(250,250,250);
  14.   smooth(2);
  15.   game = new Game(activePlayer, status, gameMode);
  16. }
  17.  
  18. void draw(){
  19.   game.drawGameArea();
  20.   game.gameModeType();
  21.   game.statusType();
  22. }
  23.  
  24. void keyPressed(){
  25.   game.onKeyPress();
  26. }
  27.  
  28. void mousePressed(){
  29.   game.onMousePress();
  30.   game.checkWinner();
  31. }
  32.  
  33. class Game{
  34.   private int activePlayer;
  35.   private int status;
  36.   private int gameMode;
  37.   private Field[][] gameArea;
  38.  
  39.   Game(int activePlayer, int status, int gameMode){
  40.     this.activePlayer = activePlayer;
  41.     this.status = status;
  42.     this.gameMode = gameMode;
  43.     this.createGameArea();
  44.   }
  45.  
  46.   void setActivePlayer(int player) { this.activePlayer = player; }
  47.   void setStatus(int status) { this.status = status; }
  48.   void setGameMode(int mode) { this.gameMode = mode; }
  49.   int getActivePlayer() { return this.activePlayer; }
  50.   int getStatus() { return this.status; }
  51.   int getGameMode() { return this.gameMode; }
  52.  
  53.   void createGameArea(){
  54.     gameArea = new Field[3][3];
  55.     for(int i = 0; i < 3; i++){
  56.       for(int j = 0; j < 3; j++){
  57.         this.gameArea[i][j] = new Field(width / 3 * i, 30 + width / 3 * j, width / 3, width / 3);
  58.       }
  59.     }
  60.   }
  61.  
  62.   void drawGameArea(){
  63.     for(int i = 0; i < 3; i++){
  64.       for(int j = 0; j < 3; j++){
  65.         this.gameArea[i][j].draw();
  66.       }
  67.     }
  68.   }
  69.  
  70.   void gameModeType(){
  71.     int r1, r2;
  72.     if(gameMode == 0){
  73.       if(activePlayer == 1 && status == 0){
  74.        do{
  75.          r1 = int(random(0,3));
  76.          r2 = int(random(0,3));
  77.          if(!gameArea[r1][r2].haveOwner()){
  78.            gameArea[r1][r2].setOwner(2);
  79.            gameArea[r1][r2].draw();
  80.            checkWinner();
  81.            switchPlayer();
  82.            break;
  83.          }
  84.        }while(true);
  85.       }
  86.     }
  87.   }
  88.  
  89.   void statusType(){
  90.     if(status == 1){
  91.       fill(0);
  92.       textSize(15);
  93.       smooth();
  94.       text("Wciśnij dowolny przycisk by zrestartować grę", width / 2 - width / 3, height - 40);
  95.       fill(250);
  96.     }
  97.   }
  98.  
  99.   void onKeyPress(){
  100.     println("key pressed " + keyCode + ", current game status: " + this.status);
  101.     if(status == 1){
  102.         for(int i = 0; i < 3; i++){
  103.           for(int j = 0; j < 3; j++){
  104.             gameArea[i][j] = null;
  105.             gameArea[i][j] = new Field(width / 3 * i, 30 + width / 3 * j, width / 3, width / 3);
  106.             gameArea[i][j].setOwner(0);
  107.             background(250,250,250);
  108.             rect(-5,-5,700,700);
  109.             status = 0;
  110.           }
  111.         }
  112.         println("New game created");
  113.       }
  114.   }
  115.  
  116.   void onMousePress(){
  117.     if(status == 0){
  118.       if(gameMode == 0 && activePlayer == 2){
  119.         for(int i = 0; i < 3; i++){
  120.           for(int j = 0; j < 3; j++){
  121.             gameArea[i][j].onClick(mouseX, mouseY, game);
  122.           }
  123.         }
  124.       }
  125.       else if(gameMode == 1){
  126.         for(int i = 0; i < 3; i++){
  127.           for(int j = 0; j < 3; j++){
  128.             gameArea[i][j].onClick(mouseX, mouseY, game);
  129.           }
  130.         }
  131.       }
  132.     }
  133.   }
  134.  
  135.   boolean isDraw(){
  136.     int avalaibleFields = 0;
  137.     for(int i = 0; i < 3; i++){
  138.       for(int j = 0; j < 3; j++){
  139.         if(gameArea[i][j].haveOwner()) avalaibleFields++;
  140.       }
  141.     }
  142.    
  143.     if(avalaibleFields == 9) { this.status = 1;  return true; }
  144.     else return false;
  145.   }
  146.  
  147.   void checkWinner(){
  148.    if(this.getWinner()>0 || this.isDraw()) this.status = 1;
  149.   }
  150.  
  151.   void switchPlayer(){
  152.     if(this.activePlayer == 2) this.activePlayer = 1;
  153.     else this.activePlayer = 2;
  154.     println("Player switched: " + this.activePlayer);
  155.   }
  156.  
  157.   int getWinner(){
  158.     int probablyWinner;
  159.    
  160.     //Cols
  161.     for(int i = 0; i < 3; i++){
  162.       probablyWinner = gameArea[i][0].getOwner();
  163.       if(probablyWinner != 0){
  164.         if(probablyWinner == gameArea[i][1].getOwner() && probablyWinner == gameArea[i][2].getOwner()){
  165.           drawLine(gameArea[i][0], gameArea[i][2], 2);
  166.           return probablyWinner;
  167.         }
  168.       }  
  169.     }
  170.    
  171.     //Rows
  172.     for(int i = 0; i < 3; i++){
  173.       probablyWinner = gameArea[0][i].getOwner();
  174.       if(probablyWinner != 0){
  175.         if(probablyWinner == gameArea[1][i].getOwner() && probablyWinner == gameArea[2][i].getOwner()){
  176.           drawLine(gameArea[0][i], gameArea[2][i], 1);
  177.           return probablyWinner;
  178.         }
  179.       }  
  180.     }
  181.    
  182.     //Crossed 1
  183.       probablyWinner = gameArea[0][0].getOwner();
  184.       if(probablyWinner != 0){
  185.         if(probablyWinner == gameArea[1][1].getOwner() && probablyWinner == gameArea[2][2].getOwner()){
  186.           drawLine(gameArea[0][0], gameArea[2][2], 3);
  187.           return probablyWinner;
  188.         }
  189.       }  
  190.    
  191.     //Crossed 2
  192.       probablyWinner = gameArea[0][2].getOwner();
  193.       if(probablyWinner != 0){
  194.         if(probablyWinner == gameArea[1][1].getOwner() && probablyWinner == gameArea[2][0].getOwner()){
  195.           drawLine(gameArea[0][2], gameArea[2][0], 4);
  196.           return probablyWinner;
  197.         }
  198.       }  
  199.    
  200.     return 0;
  201.   }
  202.  
  203.   void drawLine(Field f1, Field f2, int type){
  204.     int x1 = f1.getPosX();
  205.     int y1 = f1.getPosY();
  206.     int x2 = f2.getPosX();
  207.     int y2 = f2.getPosY();
  208.     int w = f1.getWidth();
  209.     int h = f1.getHeight();
  210.     stroke(0,0,0);
  211.     switch(type){
  212.       case 1:
  213.         line(x1,y1+(h/2),x2+w,y2+(h/2));
  214.         break;
  215.       case 2:
  216.          line(x1+(w/2),y1,x2+(w/2),y2+h);
  217.          break;
  218.       case 3:
  219.          line(x1,y1,x2+w,y2+h);
  220.          break;
  221.       case 4:
  222.          line(x1,y1+h,x2+w,y2);
  223.          break;
  224.     }
  225.   }
  226. }
  227.  
  228. class Field{
  229.   private int x, y, w, h, owner;
  230.  
  231.   Field(int posX, int posY, int fieldWidth, int fieldHeight){
  232.     this.x = posX;
  233.     this.y = posY;
  234.     this.w = fieldWidth;
  235.     this.h = fieldHeight;
  236.   }
  237.  
  238.   void setOwner(int player){ this.owner = player; }  
  239.   int getPosX(){ return this.x; }  
  240.   int getPosY(){ return this.y; }  
  241.   int getHeight(){ return this.h; }  
  242.   int getWidth(){ return this.w; }
  243.   int getOwner(){ return this.owner; }  
  244.  
  245.   void draw(){
  246.     noFill();
  247.     stroke(80);
  248.     strokeWeight(5);
  249.     rect(x, y, w, h);
  250.     stroke(150,0,150, 160);
  251.    
  252.     if(getOwner() == 1){
  253.       stroke(200,0,0);
  254.       ellipseMode(CORNER);
  255.       ellipse(x+30, y+30, w-60, w-60);
  256.     }
  257.     else if(getOwner() == 2){
  258.       stroke(0,150,0);
  259.       line(x+40, y+40, x+w-40, y+h-40);
  260.       line(x+w-40, y+40, x+40, y+h-40);
  261.     }
  262.     else{
  263.     }
  264.   }
  265.  
  266.   void onClick(int x1, int y1, Game game){
  267.     if(!haveOwner()){
  268.       if(x1 > this.x && x1 < this.x + this.w && y1 > this.y && y1 < this.y + this.h){
  269.          game.switchPlayer();
  270.          this.setOwner(game.getActivePlayer());
  271.       }
  272.     }
  273.   }
  274.  
  275.   boolean haveOwner(){
  276.     if(owner==0) return false;
  277.     else return true;
  278.   }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement