Advertisement
manudude03

tic tac toe 2

Oct 25th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.61 KB | None | 0 0
  1. //Main.java
  2. import javax.swing.JFrame; // for creating windows
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         JFrame window= new JFrame();
  7.         window.setSize(800, 600);
  8.         window.setTitle("Tic-tac-toe");
  9.         window.setVisible(true);
  10.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.         Game game=new Game();
  12.         //Board board= new Board();
  13.         window.add(game);
  14.     }
  15. }
  16.  
  17. //Board.java (basically a trimmed down version from before separating the game from the graph
  18. import javax.swing.*;
  19. import java.awt.*; // wildcard first level only
  20. import java.awt.geom.*; // drawing
  21. //import java.awt.event.*;
  22. public class Board extends JComponent{
  23.     /* method paintConponent
  24.      * inherited from JComponent
  25.      * non-Javadoc)
  26.      * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
  27.      */
  28.     public void paintComponent(Graphics g)
  29.     {
  30.         Graphics2D g2= (Graphics2D) g;
  31.         // create background
  32.         g2.setColor(Color.BLUE);
  33.         g2.fillRect(0, 0, this.getWidth(), this.getHeight());
  34.         g2.setColor(Color.WHITE);
  35.         // first vertical line of board
  36.         Line2D.Double v1=new Line2D.Double(350, 150, 350, 450);
  37.         g2.draw(v1);
  38.        
  39.         // second vertical line of board
  40.         Line2D.Double v2=new Line2D.Double(450, 150, 450, 450);
  41.         g2.draw(v2);
  42.        
  43.         // first horizontal line of board
  44.         Line2D.Double h1=new Line2D.Double(250, 250, 550, 250);
  45.         g2.draw(h1);
  46.        
  47.         // second horizontal line of board
  48.         Line2D.Double h2=new Line2D.Double(250, 350, 550, 350);
  49.         g2.draw(h2);
  50.        
  51.         // creating the content for the 9 cells;
  52.         Font text= new Font("serif", Font.BOLD, 72);
  53.         g2.setFont(text);
  54.         for (int i=0; i<9; i++)
  55.         {
  56.             // type-casting required as drawstring required ints while x and y in cells are doubles
  57.             g2.drawString(Game.cells[i].getContent(), (int)Game.cells[i].getx(), (int)Game.cells[i].gety());
  58.         }
  59.         // change font for bottom text
  60.         Font textstatus= new Font("serif", Font.PLAIN, 16);
  61.         g2.setFont(textstatus);
  62.         g2.drawString(Game.status, 350, 550);
  63.     }
  64. }
  65.  
  66. //Game.java (contains all the game info from the old Board.java)
  67.  
  68. // displays key information about game
  69. public class Game extends Board{
  70.     public static String move="X";
  71.     public static String status="Player X's move";
  72.     // the board contains the 9 cells which need drawn
  73.     public static Cell[] cells = new Cell[9];
  74.     public static int state=0;
  75.     public Game()
  76.     {
  77.         // create the cells here
  78.         int x,y,xlower,ylower,xupper,yupper,id;
  79.         for (int i=0; i<3; i++)
  80.         {
  81.             for (int j=0; j<3; j++)
  82.             {
  83.                 x=274+100*i;
  84.                 y=174+100*j;
  85.                 xlower=250+100*i;
  86.                 ylower=150+100*j;
  87.                 xupper=350+100*i;
  88.                 yupper=250+100*j;
  89.                 id=3*i+j;
  90.                 Game.cells[id]=new Cell(x,y,xlower,ylower,xupper,yupper,id,"");
  91.                 addMouseListener(Game.cells[id]);
  92.             }
  93.         }
  94.     }
  95.     public void GameState()
  96.     {
  97.         // standard iterator
  98.         int i=0;
  99.         // WinCombos is a 2D array showing the 8 possible ways of winning
  100.         int[][] WinCombos= {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,3,8},{0,4,8},{2,4,6}};
  101.         // iterate through the above array
  102.         for (i=0; i<8; i++)
  103.         {
  104.             // these 2 if statements should be combined, but this way leads to shorter lines
  105.             if (Game.cells[WinCombos[i][0]].getContent()==Game.cells[WinCombos[i][1]].getContent())
  106.             {
  107.                 if (Game.cells[WinCombos[i][0]].getContent()==Game.cells[WinCombos[i][2]].getContent())
  108.                 {
  109.                     // if it's got in here, then one of the winning combos has happened, check who won
  110.                     if (Game.cells[WinCombos[i][0]].getContent()=="X")
  111.                     {
  112.                         Game.status="Player X wins";
  113.                         Game.state=1;
  114.                         this.repaint();
  115.                         return;
  116.                     }
  117.                     if (Game.cells[WinCombos[i][0]].getContent()=="O")
  118.                     {
  119.                         Game.status="Player O wins";
  120.                         Game.state=2;
  121.                         this.repaint();
  122.                         return;
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.         // if no-one has won, is the game still ongoing?
  128.         for (i=0; i<9; i++)
  129.         {
  130.             if (Game.cells[i].getContent()=="")
  131.             {  
  132.                 Game.status="Player "+Game.move+"'s turn";
  133.                 Game.state=0; // game still in progress
  134.                 this.repaint();
  135.                 return;
  136.             }
  137.         }
  138.         Game.status="Draw";
  139.         Game.state=3;
  140.         this.repaint();
  141.         return; // game drawn
  142.     }
  143. }
  144.  
  145. //Cell.java
  146. import java.awt.event.MouseEvent;
  147. import java.awt.event.MouseListener;
  148.  
  149. import javax.swing.event.MouseInputListener;
  150.  
  151. public class Cell implements MouseListener, MouseInputListener{
  152.    
  153.     private String content;
  154.     private double x;
  155.     private double y;
  156.     private double xlower;
  157.     private double ylower;
  158.     private double xupper;
  159.     private double yupper;
  160.     public int id;
  161.     /* constructor Cell:
  162.      * parameters:-
  163.      * x: x co-ordinate for the text
  164.      * y: y-co-ordinate for the text
  165.      * xlower: the lower x bound for the cell
  166.      * ylower: the lower y bound for the cell
  167.      * xupper: the upper x bound for the cell
  168.      * yupper: the upper y bound for the cell
  169.      * text: what the cell should display
  170.      *
  171.      */
  172.     public Cell(double x, double y, double xlower, double ylower, double xupper, double yupper, int id, String text)
  173.     {
  174.         this.x=x;
  175.         this.y=y;
  176.         this.xlower=xlower;
  177.         this.ylower=ylower;
  178.         this.xupper=xupper;
  179.         this.yupper=yupper;
  180.         this.id=id;
  181.         this.content=text;
  182.     }
  183.     public void setx(double x)
  184.     {
  185.         this.x=x;
  186.     }
  187.     public double getx()
  188.     {
  189.         return this.x;
  190.     }
  191.     public void sety(double y)
  192.     {
  193.         this.y=y;
  194.     }
  195.     public double gety()
  196.     {
  197.         return this.y;
  198.     }
  199.     public void setContent(String text)
  200.     {
  201.         this.content=text;
  202.     }
  203.     public String getContent()
  204.     {
  205.         return this.content;
  206.     }
  207.     // upper and lower bounds are never changed hence no setter function
  208.     public double getxlower()
  209.     {
  210.         return this.xlower;
  211.     }
  212.     public double getylower()
  213.     {
  214.         return this.ylower;
  215.     }
  216.     public double getxupper()
  217.     {
  218.         return this.xupper;
  219.     }
  220.     public double getyupper()
  221.     {
  222.         return this.yupper;
  223.     }
  224.     @Override
  225.     public void mouseDragged(MouseEvent arg0) {
  226.         // TODO Auto-generated method stub
  227.        
  228.     }
  229.     @Override
  230.     public void mouseMoved(MouseEvent arg0) {
  231.         // TODO Auto-generated method stub
  232.        
  233.     }
  234.     @Override
  235.     public void mouseClicked(MouseEvent e) {
  236.         // TODO Auto-generated method stub
  237.         System.out.println("Cell "+this.id+" clicked");
  238.         if (this.getContent()!="")
  239.         {
  240.             this.setContent(Game.move);
  241.             if (Game.move=="X")
  242.             {
  243.                 Game.move="O";
  244.             }
  245.             else
  246.             {
  247.                 Game.move="X";
  248.             }
  249.             Game.cells[this.id].setContent(Game.move);
  250.         }
  251.     }
  252.     @Override
  253.     public void mouseEntered(MouseEvent e) {
  254.         // TODO Auto-generated method stub
  255.        
  256.     }
  257.     @Override
  258.     public void mouseExited(MouseEvent e) {
  259.         // TODO Auto-generated method stub
  260.        
  261.     }
  262.     @Override
  263.     public void mousePressed(MouseEvent e) {
  264.         // TODO Auto-generated method stub
  265.        
  266.     }
  267.     @Override
  268.     public void mouseReleased(MouseEvent e) {
  269.         // TODO Auto-generated method stub
  270.        
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement