Advertisement
brilliant_moves

NoughtsAndCrosses.java

Jun 4th, 2013
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.91 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class NoughtsAndCrosses {
  6.     public static void main(String[] args) {
  7.         NoughtsAndCrossesJFrame frame = new NoughtsAndCrossesJFrame();
  8.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  9.         // Set to a reasonable size.
  10.         frame.setSize(1000, 700);
  11.         frame.setVisible(true);
  12.     }
  13. }
  14.  
  15. class NoughtsAndCrossesJFrame extends JFrame implements ActionListener, MouseListener {
  16.  
  17.     /**
  18.     *   Program:    NoughtsAndCrosses.java
  19.     *   Purpose:    2 player game of Xs and Os (also known as "Tic-Tac-Toe").
  20.     *   Creator:    Chris Clarke
  21.     *   Created:    17.12.2006
  22.     *   Modified:   05.05.2013 Updated to JFrame
  23.     */ 
  24.  
  25.     private Label       lbl1;
  26.     private Choice      ch1;
  27.     private Button      btnPlay;
  28.     int[][]         grid;       // 3 by 3 grid
  29.     int         sqr_X, sqr_Y;   // the co-ordinates of square in the grid
  30.     int         toPlay=-1;
  31.     boolean         paint=true;
  32.     public static final int NOUGHTS = 0;
  33.     public static final int CROSSES = 1;
  34.     public static final int STARTX = 200;
  35.     public static final int STARTY = 200;
  36.     public static final int SQUARESIZE = 100;
  37.    
  38.     public NoughtsAndCrossesJFrame() {
  39.         setTitle("Noughts And Crosses by Chris Clarke");
  40.         // Build control panel.
  41.         Panel panel = new Panel(); // Uses flow layout
  42.  
  43.         lbl1 = new Label("Who goes first >>>");
  44.         panel.add(lbl1);
  45.  
  46.         ch1 = new Choice();
  47.         ch1.add("Noughts 0");
  48.         ch1.add("Crosses X");
  49.         panel.add(ch1);
  50.  
  51.         btnPlay = new Button("New Game");
  52.         btnPlay.addActionListener(this);
  53.         panel.add(btnPlay);
  54.  
  55.         add(panel, BorderLayout.NORTH);
  56.  
  57.         grid = new int[3][3];
  58.         clearGrid();
  59.         addMouseListener(this);
  60.         this.setFocusable(true);
  61.     }
  62.  
  63.     public void paint(Graphics g) {
  64.         // if game is won, do nothing
  65.         if (!paint) return;
  66.         boolean foundSpace=false;
  67.  
  68.         g.setColor(Color.WHITE);    // clear win/draw message
  69.         g.fillRect(STARTX+4*SQUARESIZE, STARTY+70, 400, -200);
  70.  
  71.         g.setColor(Color.BLACK);
  72.         drawEmptyGrid(g);
  73.         g.setFont(new Font("Courier", Font.PLAIN, 72));
  74.         for (int y=0; y<=2; y++) {
  75.             for (int x=0; x<=2; x++) {
  76.                 if (grid[y][x]==NOUGHTS) {
  77.                     // draw Nought
  78.                     g.drawString("O", (STARTX+25+(x*SQUARESIZE)),
  79.                      (STARTY+80+(y*SQUARESIZE)));
  80.                 } else if (grid[y][x]==CROSSES) {
  81.                     //draw Cross
  82.                     g.drawString("X", (STARTX+25+(x*SQUARESIZE)),
  83.                      (STARTY+80+(y*SQUARESIZE)));
  84.                 } else {
  85.                     g.setColor(Color.WHITE);
  86.                     g.fillRect(STARTX+1+(x*SQUARESIZE),
  87.                      STARTY+1+(y*SQUARESIZE), SQUARESIZE-1, SQUARESIZE-1);
  88.                     g.setColor(Color.BLACK);
  89.                 }//end if
  90.             }//end for x
  91.         }//end for y
  92.  
  93.         for (int shape=NOUGHTS; shape<=CROSSES; shape++)
  94.             if (isLine(g, shape)) {
  95.                 String winner;
  96.                 if (shape==NOUGHTS)
  97.                     winner="O wins";
  98.                 else
  99.                     winner="X wins";
  100.                 g.drawString(winner, STARTX+4*SQUARESIZE, STARTY+50);
  101.                 paint=false;
  102.                 return;
  103.             }
  104.         // look for blank spaces
  105.         for (int y=0; y<=2; y++)
  106.             for (int x=0; x<=2; x++)
  107.                 if (grid[y][x]==-1) foundSpace=true;
  108.         // if board is full and neither side has won
  109.         if (!foundSpace)
  110.             g.drawString("Draw", STARTX+4*SQUARESIZE, STARTY+50);
  111.     }
  112.  
  113.     public boolean isLine(Graphics g, int shape) {
  114.     // is there a line of three "x"s or "o"s (horizontal/vertical/diagonal)
  115.         g.setColor(Color.RED);
  116.         if ((grid[0][0]==shape)&&(grid[0][1]==shape)&&(grid[0][2]==shape)) {
  117.             g.drawLine(STARTX, STARTY+50, STARTX+3*SQUARESIZE, STARTY+50);
  118.             return true;
  119.         }
  120.         if ((grid[1][0]==shape)&&(grid[1][1]==shape)&&(grid[1][2]==shape)) {
  121.             g.drawLine(STARTX, STARTY+150, STARTX+3*SQUARESIZE, STARTY+150);
  122.             return true;
  123.         }
  124.         if ((grid[2][0]==shape)&&(grid[2][1]==shape)&&(grid[2][2]==shape)) {
  125.             g.drawLine(STARTX, STARTY+250, STARTX+3*SQUARESIZE, STARTY+250);
  126.             return true;
  127.         }
  128.         if ((grid[0][0]==shape)&&(grid[1][0]==shape)&&(grid[2][0]==shape)) {
  129.             g.drawLine(STARTX+50, STARTY, STARTX+50, STARTY+3*SQUARESIZE);
  130.             return true;
  131.         }
  132.         if ((grid[0][1]==shape)&&(grid[1][1]==shape)&&(grid[2][1]==shape)) {
  133.             g.drawLine(STARTX+150, STARTY, STARTX+150, STARTY+3*SQUARESIZE);
  134.             return true;
  135.         }
  136.         if ((grid[0][2]==shape)&&(grid[1][2]==shape)&&(grid[2][2]==shape)) {
  137.             g.drawLine(STARTX+250, STARTY, STARTX+250, STARTY+3*SQUARESIZE);
  138.             return true;
  139.         }
  140.         if ((grid[0][0]==shape)&&(grid[1][1]==shape)&&(grid[2][2]==shape)) {
  141.             g.drawLine(STARTX, STARTY, STARTX+3*SQUARESIZE,
  142.              STARTY+3*SQUARESIZE);
  143.             return true;
  144.         }
  145.         if ((grid[2][0]==shape)&&(grid[1][1]==shape)&&(grid[0][2]==shape)) {
  146.             g.drawLine(STARTX+3*SQUARESIZE, STARTY, STARTX,
  147.              STARTY+3*SQUARESIZE);
  148.             return true;
  149.         }
  150.         return false;
  151.     }
  152.  
  153.     public void drawEmptyGrid(Graphics g) {
  154.         ///
  155.         g.drawLine(STARTX, STARTY+SQUARESIZE, STARTX+3*SQUARESIZE,
  156.          STARTY+SQUARESIZE);
  157.         g.drawLine(STARTX, STARTY+2*SQUARESIZE, STARTX+3*SQUARESIZE,
  158.          STARTY+2*SQUARESIZE);
  159.         g.drawLine(STARTX+SQUARESIZE, STARTY, STARTX+SQUARESIZE,
  160.          STARTY+3*SQUARESIZE);
  161.         g.drawLine(STARTX+2*SQUARESIZE, STARTY, STARTX+2*SQUARESIZE,
  162.          STARTY+3*SQUARESIZE);
  163.     }
  164.  
  165.     public void clearGrid() {
  166.         for (int i=0; i<=2; i++)
  167.             for (int j=0; j<=2; j++)
  168.                 grid[i][j]=-1;
  169.     }
  170.  
  171.     public void reset() {
  172.         clearGrid();
  173.         toPlay=-1;
  174.         paint=true;
  175.         repaint();
  176.     }
  177.  
  178.     public void mouseClicked(MouseEvent event) {
  179.         if ((event.getX()<STARTX)||(event.getX()>STARTX+3*SQUARESIZE))
  180.             return;
  181.         if ((event.getY()<STARTY)||(event.getY()>STARTY+3*SQUARESIZE))
  182.             return;
  183.         sqr_X=(event.getX()-STARTX)/SQUARESIZE;
  184.         sqr_Y=(event.getY()-STARTY)/SQUARESIZE;
  185.         if (toPlay==-1)
  186.             toPlay=ch1.getSelectedIndex();
  187.         if (grid[sqr_Y][sqr_X]!=-1)
  188.             return;
  189.         grid[sqr_Y][sqr_X]=toPlay;
  190.         if (toPlay==NOUGHTS)
  191.             toPlay=CROSSES;
  192.         else
  193.             toPlay=NOUGHTS;
  194.         repaint();
  195.     }
  196.  
  197.     public void mouseEntered(MouseEvent event) {}
  198.  
  199.     public void mouseExited(MouseEvent event) {}
  200.  
  201.     public void mousePressed(MouseEvent event) {}
  202.  
  203.     public void mouseReleased(MouseEvent event) {}
  204.  
  205.     // get user involvement in menu item
  206.     public void actionPerformed(ActionEvent e) {
  207.         if (e.getSource() == btnPlay)
  208.             reset();
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement