Advertisement
manudude03

tic tac toe 1

Oct 25th, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.55 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.         Board board= new Board();
  12.         window.add(board);
  13.     }
  14. }
  15.  
  16. //Board.java
  17.  
  18. import javax.swing.*;
  19. import javax.swing.event.MouseInputListener;
  20. import java.awt.*; // wildcard first level only
  21. import java.awt.event.MouseEvent;
  22. import java.awt.event.MouseListener;
  23. import java.awt.geom.*; // drawing
  24. //import java.awt.event.*;
  25. public class Board extends JComponent implements MouseListener, MouseInputListener{
  26.     // the board contains the 9 cells which need drawn
  27.     public Cell[] cells = new Cell[9];
  28.    
  29.     // status is the message on the bottom of the window
  30.     public String status= "Player X's move";
  31.     // move determines whose turn it is
  32.     public String move="X";
  33.     public Board()
  34.     {
  35.         int x,y, xlower, xupper, ylower, yupper;
  36.         for (int i=0; i<3; i++)
  37.         {
  38.             for (int j=0; j<3; j++)
  39.             {
  40.                 x=276+i*100; // where the marker is to be put
  41.                 y=224+j*100;
  42.                 xlower= 250+i*100; // top left corner of area
  43.                 ylower= 150+j*100;
  44.                 xupper= 350+i*100; // bottom right corner of area
  45.                 yupper= 250+j*100;
  46.                 // Cells need to be created now to allow for screen refreshes
  47.                 this.cells[i+j*3]=new Cell(x,y,xlower,ylower,xupper,yupper,"");
  48.             }
  49.         }
  50.         addMouseListener(this); // adds listener to cover the entire board (screen)
  51.     }
  52.     /* method paintConponent
  53.      * inherited from JComponent
  54.      * non-Javadoc)
  55.      * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
  56.      */
  57.     public void paintComponent(Graphics g)
  58.     {
  59.         Graphics2D g2= (Graphics2D) g;
  60.         // create background
  61.         g2.setColor(Color.BLUE);
  62.         g2.fillRect(0, 0, this.getWidth(), this.getHeight());
  63.         g2.setColor(Color.WHITE);
  64.         // first vertical line of board
  65.         Line2D.Double v1=new Line2D.Double(350, 150, 350, 450);
  66.         g2.draw(v1);
  67.        
  68.         // second vertical line of board
  69.         Line2D.Double v2=new Line2D.Double(450, 150, 450, 450);
  70.         g2.draw(v2);
  71.        
  72.         // first horizontal line of board
  73.         Line2D.Double h1=new Line2D.Double(250, 250, 550, 250);
  74.         g2.draw(h1);
  75.        
  76.         // second horizontal line of board
  77.         Line2D.Double h2=new Line2D.Double(250, 350, 550, 350);
  78.         g2.draw(h2);
  79.        
  80.         // creating the content for the 9 cells;
  81.         Font text= new Font("serif", Font.BOLD, 72);
  82.         g2.setFont(text);
  83.         for (int i=0; i<9; i++)
  84.         {
  85.             // type-casting required as drawstring required ints while x and y in cells are doubles
  86.             g2.drawString(this.cells[i].getContent(), (int)this.cells[i].getx(), (int)this.cells[i].gety());
  87.         }
  88.         // change font for bottom text
  89.         Font textstatus= new Font("serif", Font.PLAIN, 16);
  90.         g2.setFont(textstatus);
  91.         g2.drawString(status, 350, 550);
  92.     }
  93.     /*
  94.      * function ValidMove
  95.      * paramters: id (int)
  96.      * determines if the selected move is legal
  97.      * returns boolean
  98.      */
  99.     public boolean ValidMove(int id)
  100.     {
  101.       // check if id is out of range, which would otherwise cause out of bounds error (shouldn't ever be the case)
  102.       if (id>8 || id<0) return false;
  103.       // make sure the selected cell hasn't already been taken
  104.       if (this.cells[id].getContent()!="") return false;
  105.       // if neither of these tests fail, the move is valid
  106.       return true;
  107.     }
  108.     /*
  109.      * function GameState
  110.      * parameters: none
  111.      * determines the current state of the game
  112.      * returns int (0=ongoing, 1=X wins, 2=O wins, 3=drawn)
  113.      */
  114.     public int GameState()
  115.     {
  116.         // standard iterator
  117.         int i=0;
  118.         // WinCombos is a 2D array showing the 8 possible ways of winning
  119.         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}};
  120.         // iterate through the above array
  121.         for (i=0; i<8; i++)
  122.         {
  123.             // these 2 if statements should be combined, but this way leads to shorter lines
  124.             if (this.cells[WinCombos[i][0]].getContent()==this.cells[WinCombos[i][1]].getContent())
  125.             {
  126.                 if (this.cells[WinCombos[i][0]].getContent()==this.cells[WinCombos[i][2]].getContent())
  127.                 {
  128.                     // if it's got in here, then one of the winning combos has happened, check who won
  129.                     if (this.cells[WinCombos[i][0]].getContent()=="X") return 1; // player X wins
  130.                     if (this.cells[WinCombos[i][0]].getContent()=="O") return 2; // player O wins
  131.                 }
  132.             }
  133.         }
  134.         // if no-one has won, is the game still ongoing?
  135.         for (i=0; i<9; i++)
  136.         {
  137.             if (this.cells[i].getContent()=="") return 0; // game still in progress
  138.         }
  139.         return 3; // game drawn
  140.     }
  141.     // required by interface, but not used
  142.     @Override
  143.     public void mouseDragged(MouseEvent arg0) {
  144.         // TODO Auto-generated method stub
  145.        
  146.     }
  147.     // required by interface, but not used
  148.     @Override
  149.     public void mouseMoved(MouseEvent arg0) {
  150.         // TODO Auto-generated method stub
  151.        
  152.     }
  153.     // this function is where the game does whatever it has to do for each turn.
  154.     @Override
  155.     public void mouseClicked(MouseEvent e) {
  156.         // TODO Auto-generated method stub
  157.         // find where on the screen the click heppened
  158.         double x=e.getX();
  159.         double y=e.getY();
  160.         int state;
  161.         if (this.GameState()==0)
  162.         {
  163.             for (int i=0; i<9; i++)
  164.             {
  165.                 // check each region to see if the click happened inside one of the cells
  166.                 // using > and < rather than >= and <= to guard against selecting the wrong cell
  167.                 if(x>this.cells[i].getxlower() && x<this.cells[i].getxupper())
  168.                 {                  
  169.                     // this if could be combined with the above, but neater this way
  170.                     if (y>this.cells[i].getylower() && y<this.cells[i].getyupper())
  171.                     {
  172.                         // we have found the cell clicked, now see if move is valid
  173.                         if (this.ValidMove(i))
  174.                         {
  175.                             // change the content of the cell
  176.                             this.cells[i].setContent(move);
  177.                             // switch turns
  178.                             if (move=="X") move="O";
  179.                             else move="X";
  180.                             // check the current state of the game
  181.                             state=this.GameState();
  182.                             if (state==1)
  183.                             {
  184.                                 // player X has won
  185.                                 status="Player X wins";
  186.                             }
  187.                             else if (state==2)
  188.                             {
  189.                                 // player O has won
  190.                                 status="Player O wins";
  191.                             }
  192.                             else if (state==3)
  193.                             {
  194.                                 // all moves have been made, no winner
  195.                                 status="Draw";
  196.                             }
  197.                             else
  198.                             {
  199.                                 // game still ongoing
  200.                                 status="Player "+move+"\'s move.";
  201.                             }
  202.                             // refreshes the screen
  203.                             repaint();
  204.                             // prevent any more iterations to save processing time, they would be false anyway
  205.                             break;
  206.                         }
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.     }
  212.     // required by interface, but not used
  213.     @Override
  214.     public void mouseEntered(MouseEvent arg0) {
  215.         // TODO Auto-generated method stub
  216.        
  217.     }
  218.     // required by interface, but not used
  219.     @Override
  220.     public void mouseExited(MouseEvent arg0) {
  221.         // TODO Auto-generated method stub
  222.        
  223.     }
  224.     // required by interface, but not used
  225.     @Override
  226.     public void mousePressed(MouseEvent arg0) {
  227.         // TODO Auto-generated method stub
  228.        
  229.     }
  230.     // required by interface, but not used
  231.     @Override
  232.     public void mouseReleased(MouseEvent arg0) {
  233.         // TODO Auto-generated method stub
  234.        
  235.     }
  236. }
  237.  
  238. //Cell.java
  239.  
  240. import java.awt.event.MouseEvent;
  241. import java.awt.event.MouseListener;
  242.  
  243. import javax.swing.event.MouseInputListener;
  244.  
  245. public class Cell{
  246.    
  247.     private String content;
  248.     private double x;
  249.     private double y;
  250.     private double xlower;
  251.     private double ylower;
  252.     private double xupper;
  253.     private double yupper;
  254.    
  255.     /* constructor Cell:
  256.      * parameters:-
  257.      * x: x co-ordinate for the text
  258.      * y: y-co-ordinate for the text
  259.      * xlower: the lower x bound for the cell
  260.      * ylower: the lower y bound for the cell
  261.      * xupper: the upper x bound for the cell
  262.      * yupper: the upper y bound for the cell
  263.      * text: what the cell should display
  264.      *
  265.      */
  266.     public Cell(double x, double y, double xlower, double ylower, double xupper, double yupper, String text)
  267.     {
  268.         this.x=x;
  269.         this.y=y;
  270.         this.xlower=xlower;
  271.         this.ylower=ylower;
  272.         this.xupper=xupper;
  273.         this.yupper=yupper;
  274.         this.content=text;
  275.     }
  276.     public void setx(double x)
  277.     {
  278.         this.x=x;
  279.     }
  280.     public double getx()
  281.     {
  282.         return this.x;
  283.     }
  284.     public void sety(double y)
  285.     {
  286.         this.y=y;
  287.     }
  288.     public double gety()
  289.     {
  290.         return this.y;
  291.     }
  292.     public void setContent(String text)
  293.     {
  294.         this.content=text;
  295.     }
  296.     public String getContent()
  297.     {
  298.         return this.content;
  299.     }
  300.     // upper and lower bounds are never changed hence no setter function
  301.     public double getxlower()
  302.     {
  303.         return this.xlower;
  304.     }
  305.     public double getylower()
  306.     {
  307.         return this.ylower;
  308.     }
  309.     public double getxupper()
  310.     {
  311.         return this.xupper;
  312.     }
  313.     public double getyupper()
  314.     {
  315.         return this.yupper;
  316.     }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement