Advertisement
Guest User

Ultimate TicTacToe Project

a guest
May 28th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.60 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.awt.*;
  4. import java.applet.*;
  5. import java.awt.event.*;
  6.  
  7. //I'm so sorry about how gross this looks. Problem is when playing the rectangles correlating to the left middle 3x3 aren't working properly. Rules are you play in the quadrant of the larger 3x3 as indicated by the where the last person played.
  8.  
  9.  
  10. public class Backup extends Applet implements MouseListener {
  11.   Graphics buffer;
  12.   Image img;
  13.   protected int height, width;
  14.   Rectangle[][] rectangles = new Rectangle[9][9];
  15.   int[][] squarecheck = new int[9][9];
  16.   //False = X, True = O
  17.   boolean turn = false;
  18.   boolean clicked = false;
  19.   boolean tleft, tright, tmid, mright, mleft, bright, bmid, bleft;
  20.   boolean mid = true;
  21.   int x,y;
  22.  
  23.  
  24.  
  25.   public void init() {
  26.     //0 = Empty
  27.     //1 = X's
  28.     //2 = O's
  29.     for (int i = 0; i < 9; i++) {
  30.       for (int j = 0; j < 9; j++) {
  31.         squarecheck[i][j] = 0;
  32.       }
  33.     }
  34.     addMouseListener(this);
  35.   }
  36.   // Turn Announcer
  37.   public void paint(Graphics g) {
  38.     height = getSize().height;
  39.     width = getSize().width;
  40.     if (turn == false) {
  41.       g.setColor(Color.white);
  42.       g.fillRect(0,0,80,9);
  43.       g.setColor(Color.black);
  44.       g.drawString("It is X's turn",0,9);
  45.     } else {
  46.       g.setColor(Color.white);
  47.       g.fillRect(0,0,80,9);
  48.       g.setColor(Color.black);
  49.       g.drawString("It is O's turn",0,9);
  50.     }
  51.     // Makes Board
  52.     for (int x = 0; x <= 8; x++) {
  53.       for (int y = 0; y <= 8; y++) {
  54.         if ((x == 3 && y == 0) ||(x==6 && y == 0) || (x==0 && y == 3) || (x==0 && y==6)) {
  55.           g.setColor(Color.red);
  56.           ((Graphics2D)g).setStroke(new BasicStroke(3.0f));
  57.           g.drawLine(width/9*x,0,width/9*x,height);
  58.           g.drawLine(0,height/9*y,width,height/9*y);
  59.         } else {
  60.           g.setColor(Color.black);
  61.           ((Graphics2D)g).setStroke(new BasicStroke(1.0f));
  62.           g.drawLine(width/9*x,0,width/9*x,height);
  63.           g.drawLine(0,height/9*y,width,height/9*y);
  64.         }
  65.       }
  66.     }
  67.     // Makes Rectangle
  68.     for (int x = 0; x <= 8; x++) {
  69.       for (int y = 0; y <= 8; y++){
  70.         if (x <= 2 && y <= 2 && tleft == true) {
  71.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  72.         } else if (y <= 2 && (x <= 5 && x >= 3) && tmid == true) {
  73.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  74.         } else if (y <= 2 && (x <= 8 && x >= 6) && tright == true) {
  75.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  76.         } else if ((y <= 5 && y >= 3) && (x <= 8 && x >= 6) && mright == true) {
  77.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  78.         } else if ((y <= 5 && y >= 3) && (x <= 5 && x >= 3) && mid == true) {
  79.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  80.         } else if ((y <= 5 && y >= 3) && (x <= 0 && x >= 2) && mleft == true) {
  81.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  82.         } else if ((y <= 8 && y >= 6) && (x <= 8 && x >= 6) && bright == true) {
  83.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  84.         } else if ((y <= 8 && y >= 6) && (x <= 5 && x >= 3) && bmid == true) {
  85.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  86.         } else if ((y <= 8 && y >= 6) && (x <= 2 && x >= 0) && bleft == true) {
  87.           rectangles[x][y] = new Rectangle(width/9*x,height/9*y,width/9,height/9);
  88.         } else {
  89.           rectangles[x][y] = new Rectangle(-20,-20,1,1);
  90.         }
  91.       }
  92.     }
  93.    
  94.    
  95.    
  96.    
  97.     //Drawing X's and O's 2P
  98.     /*   if (clicked == true) {
  99.      if (turn == true) {
  100.      g.drawLine(x-width/36,y-height/36,x+width/36,y+height/36);
  101.      g.drawLine(x-width/36,y+height/36,x+width/36,y-height/36);
  102.      clicked = false;
  103.      }
  104.      if (turn == false) {
  105.      g.drawOval(x-width/36,y-height/36,width/36*2,height/36*2);
  106.      clicked = false;
  107.      }
  108.      }
  109.      */
  110.    
  111.     //Drawing X's and O'x
  112.     for (int x = 0; x < squarecheck.length; x++) {
  113.       for (int y = 0; y < squarecheck[0].length; y++) {
  114.         if (squarecheck[x][y] == 1) {
  115.           g.drawLine(x*width/9+width/27,y*height/9+height/27,x*width/9+width*2/27,y*height/9+height*2/27);
  116.           g.drawLine(x*width/9+width/27,y*height/9+height*2/27,x*width/9+width*2/27,y*height/9+height/27);
  117.         }
  118.         if (squarecheck[x][y] == 2) {
  119.           g.drawOval(x*width/9+width/27,y*height/9+height/27,width/27,height/27);
  120.         }
  121.        
  122.        
  123.        
  124.       }
  125.     }
  126.     repaint();
  127.    
  128.   }
  129.  
  130.   public void mousePressed(MouseEvent e) {
  131.   }
  132.   public void mouseReleased(MouseEvent e) {
  133.   }
  134.   public void mouseClicked(MouseEvent e) {
  135.     x = e.getX();
  136.     y = e.getY();
  137.     clicked = true;
  138.     for (int i = 0; i <= 8; i++) {
  139.       for (int j = 0; j <= 8; j++) {
  140.          if (rectangles[i][j].contains(x,y) &&  (j % 3 == 0 || j % 3 == 3) && (i % 3 == 3 || i % 3 == 0) && (squarecheck[i][j] == 0)) {
  141.           tleft = true;
  142.           tright = false;
  143.           tmid = false;
  144.           mright = false;
  145.           mid = false;
  146.           mleft = false;
  147.           bright = false;
  148.           bmid = false;
  149.           bleft = false;
  150.         }
  151.         if (rectangles[i][j].contains(x,y) &&  (i % 3 == 1) &&  (j % 3 == 3 || j % 3 == 0) && (squarecheck[i][j] == 0)) {
  152.           tleft = false;
  153.           tmid = true;
  154.           tright = false;
  155.           mright = false;
  156.           mid = false;
  157.           mleft = false;
  158.           bright = false;
  159.           bmid = false;
  160.           bleft = false;
  161.         }
  162.         if (rectangles[i][j].contains(x,y) &&  (i % 3 == 2) && (j % 3 == 3 || j % 3 == 0) && (squarecheck[i][j] == 0)) {
  163.           tleft = false;
  164.           tmid = false;
  165.           tright = true;
  166.           mright = false;
  167.           mid = false;
  168.           mleft = false;
  169.           bright = false;
  170.           bmid = false;
  171.           bleft = false;
  172.         }
  173.         if (rectangles[i][j].contains(x,y) &&  (j % 3 == 1) && (i % 3 == 2) && (squarecheck[i][j] == 0)) {
  174.           tleft = false;
  175.           tright = false;
  176.           tmid = false;
  177.           mright = true;
  178.           mid = false;
  179.           mleft = false;
  180.           bright = false;
  181.           bmid = false;
  182.           bleft = false;
  183.         }
  184.         if (rectangles[i][j].contains(x,y) &&  (j % 3 == 1) && (i % 3 == 1) && (squarecheck[i][j] == 0)) {
  185.           tleft = false;
  186.           tright = false;
  187.           tmid = false;
  188.           mid = true;
  189.           mright = false;
  190.           mleft = false;
  191.           bright = false;
  192.           bmid = false;
  193.           bleft = false;
  194.         }
  195.         if (rectangles[i][j].contains(x,y) &&  (i % 3 == 3 || i % 3 == 0) && (j % 3 == 1) && (squarecheck[i][j] == 0)) {
  196.           tleft = false;
  197.           tright = false;
  198.           tmid = false;
  199.           mright = false;
  200.           mid = false;
  201.           mleft = true;
  202.           bright = false;
  203.           bmid = false;
  204.           bleft = true;
  205.         }
  206.         if (rectangles[i][j].contains(x,y) &&  (j % 3 == 2) && (i % 3 == 2) && (squarecheck[i][j] == 0)) {
  207.           tleft = false;
  208.           tright = false;
  209.           tmid = false;
  210.           mright = false;
  211.           mid = false;
  212.           mleft = false;
  213.           bright = true;
  214.           bmid = false;
  215.           bleft = false;
  216.         }
  217.         if (rectangles[i][j].contains(x,y) &&  (j % 3 == 2) && (i % 3 == 1) && (squarecheck[i][j] == 0)) {
  218.           tleft = false;
  219.           tright = false;
  220.           tmid = false;
  221.           mright = false;
  222.           mid = false;
  223.           mleft = false;
  224.           bright = false;
  225.           bmid = true;
  226.           bleft = false;
  227.         }
  228.         if (rectangles[i][j].contains(x,y) &&  (i % 3 == 0 || i % 3 == 3) && (j % 3 == 2) && (squarecheck[i][j] == 0)) {
  229.           tleft = false;
  230.           tright = false;
  231.           tmid = false;
  232.           mright = false;
  233.           mid = false;
  234.           mleft = false;
  235.           bright = false;
  236.           bmid = false;
  237.           bleft = true;
  238.         }
  239.         if (rectangles[i][j].contains(x,y) && (turn == false) && (squarecheck[i][j] == 0)) {
  240.           squarecheck[i][j] = 1;
  241.           turn = !turn;
  242.         }
  243.         if (rectangles[i][j].contains(x,y) && (turn == true) && (squarecheck[i][j] == 0)) {
  244.           squarecheck[i][j] = 2;
  245.           turn = !turn;
  246.         }
  247.        
  248.        
  249.       }  
  250.     }
  251.     repaint();
  252.   }
  253.   public void mouseExited(MouseEvent e) {
  254.   }
  255.   public void mouseEntered(MouseEvent e) {
  256.   }
  257.   public void update(Graphics g) {
  258.     paint(g);
  259.   }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement