Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class NoughtsAndCrosses {
- public static void main(String[] args) {
- NoughtsAndCrossesJFrame frame = new NoughtsAndCrossesJFrame();
- frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
- // Set to a reasonable size.
- frame.setSize(1000, 700);
- frame.setVisible(true);
- }
- }
- class NoughtsAndCrossesJFrame extends JFrame implements ActionListener, MouseListener {
- /**
- * Program: NoughtsAndCrosses.java
- * Purpose: 2 player game of Xs and Os (also known as "Tic-Tac-Toe").
- * Creator: Chris Clarke
- * Created: 17.12.2006
- * Modified: 05.05.2013 Updated to JFrame
- */
- private Label lbl1;
- private Choice ch1;
- private Button btnPlay;
- int[][] grid; // 3 by 3 grid
- int sqr_X, sqr_Y; // the co-ordinates of square in the grid
- int toPlay=-1;
- boolean paint=true;
- public static final int NOUGHTS = 0;
- public static final int CROSSES = 1;
- public static final int STARTX = 200;
- public static final int STARTY = 200;
- public static final int SQUARESIZE = 100;
- public NoughtsAndCrossesJFrame() {
- setTitle("Noughts And Crosses by Chris Clarke");
- // Build control panel.
- Panel panel = new Panel(); // Uses flow layout
- lbl1 = new Label("Who goes first >>>");
- panel.add(lbl1);
- ch1 = new Choice();
- ch1.add("Noughts 0");
- ch1.add("Crosses X");
- panel.add(ch1);
- btnPlay = new Button("New Game");
- btnPlay.addActionListener(this);
- panel.add(btnPlay);
- add(panel, BorderLayout.NORTH);
- grid = new int[3][3];
- clearGrid();
- addMouseListener(this);
- this.setFocusable(true);
- }
- public void paint(Graphics g) {
- // if game is won, do nothing
- if (!paint) return;
- boolean foundSpace=false;
- g.setColor(Color.WHITE); // clear win/draw message
- g.fillRect(STARTX+4*SQUARESIZE, STARTY+70, 400, -200);
- g.setColor(Color.BLACK);
- drawEmptyGrid(g);
- g.setFont(new Font("Courier", Font.PLAIN, 72));
- for (int y=0; y<=2; y++) {
- for (int x=0; x<=2; x++) {
- if (grid[y][x]==NOUGHTS) {
- // draw Nought
- g.drawString("O", (STARTX+25+(x*SQUARESIZE)),
- (STARTY+80+(y*SQUARESIZE)));
- } else if (grid[y][x]==CROSSES) {
- //draw Cross
- g.drawString("X", (STARTX+25+(x*SQUARESIZE)),
- (STARTY+80+(y*SQUARESIZE)));
- } else {
- g.setColor(Color.WHITE);
- g.fillRect(STARTX+1+(x*SQUARESIZE),
- STARTY+1+(y*SQUARESIZE), SQUARESIZE-1, SQUARESIZE-1);
- g.setColor(Color.BLACK);
- }//end if
- }//end for x
- }//end for y
- for (int shape=NOUGHTS; shape<=CROSSES; shape++)
- if (isLine(g, shape)) {
- String winner;
- if (shape==NOUGHTS)
- winner="O wins";
- else
- winner="X wins";
- g.drawString(winner, STARTX+4*SQUARESIZE, STARTY+50);
- paint=false;
- return;
- }
- // look for blank spaces
- for (int y=0; y<=2; y++)
- for (int x=0; x<=2; x++)
- if (grid[y][x]==-1) foundSpace=true;
- // if board is full and neither side has won
- if (!foundSpace)
- g.drawString("Draw", STARTX+4*SQUARESIZE, STARTY+50);
- }
- public boolean isLine(Graphics g, int shape) {
- // is there a line of three "x"s or "o"s (horizontal/vertical/diagonal)
- g.setColor(Color.RED);
- if ((grid[0][0]==shape)&&(grid[0][1]==shape)&&(grid[0][2]==shape)) {
- g.drawLine(STARTX, STARTY+50, STARTX+3*SQUARESIZE, STARTY+50);
- return true;
- }
- if ((grid[1][0]==shape)&&(grid[1][1]==shape)&&(grid[1][2]==shape)) {
- g.drawLine(STARTX, STARTY+150, STARTX+3*SQUARESIZE, STARTY+150);
- return true;
- }
- if ((grid[2][0]==shape)&&(grid[2][1]==shape)&&(grid[2][2]==shape)) {
- g.drawLine(STARTX, STARTY+250, STARTX+3*SQUARESIZE, STARTY+250);
- return true;
- }
- if ((grid[0][0]==shape)&&(grid[1][0]==shape)&&(grid[2][0]==shape)) {
- g.drawLine(STARTX+50, STARTY, STARTX+50, STARTY+3*SQUARESIZE);
- return true;
- }
- if ((grid[0][1]==shape)&&(grid[1][1]==shape)&&(grid[2][1]==shape)) {
- g.drawLine(STARTX+150, STARTY, STARTX+150, STARTY+3*SQUARESIZE);
- return true;
- }
- if ((grid[0][2]==shape)&&(grid[1][2]==shape)&&(grid[2][2]==shape)) {
- g.drawLine(STARTX+250, STARTY, STARTX+250, STARTY+3*SQUARESIZE);
- return true;
- }
- if ((grid[0][0]==shape)&&(grid[1][1]==shape)&&(grid[2][2]==shape)) {
- g.drawLine(STARTX, STARTY, STARTX+3*SQUARESIZE,
- STARTY+3*SQUARESIZE);
- return true;
- }
- if ((grid[2][0]==shape)&&(grid[1][1]==shape)&&(grid[0][2]==shape)) {
- g.drawLine(STARTX+3*SQUARESIZE, STARTY, STARTX,
- STARTY+3*SQUARESIZE);
- return true;
- }
- return false;
- }
- public void drawEmptyGrid(Graphics g) {
- ///
- g.drawLine(STARTX, STARTY+SQUARESIZE, STARTX+3*SQUARESIZE,
- STARTY+SQUARESIZE);
- g.drawLine(STARTX, STARTY+2*SQUARESIZE, STARTX+3*SQUARESIZE,
- STARTY+2*SQUARESIZE);
- g.drawLine(STARTX+SQUARESIZE, STARTY, STARTX+SQUARESIZE,
- STARTY+3*SQUARESIZE);
- g.drawLine(STARTX+2*SQUARESIZE, STARTY, STARTX+2*SQUARESIZE,
- STARTY+3*SQUARESIZE);
- }
- public void clearGrid() {
- for (int i=0; i<=2; i++)
- for (int j=0; j<=2; j++)
- grid[i][j]=-1;
- }
- public void reset() {
- clearGrid();
- toPlay=-1;
- paint=true;
- repaint();
- }
- public void mouseClicked(MouseEvent event) {
- if ((event.getX()<STARTX)||(event.getX()>STARTX+3*SQUARESIZE))
- return;
- if ((event.getY()<STARTY)||(event.getY()>STARTY+3*SQUARESIZE))
- return;
- sqr_X=(event.getX()-STARTX)/SQUARESIZE;
- sqr_Y=(event.getY()-STARTY)/SQUARESIZE;
- if (toPlay==-1)
- toPlay=ch1.getSelectedIndex();
- if (grid[sqr_Y][sqr_X]!=-1)
- return;
- grid[sqr_Y][sqr_X]=toPlay;
- if (toPlay==NOUGHTS)
- toPlay=CROSSES;
- else
- toPlay=NOUGHTS;
- repaint();
- }
- public void mouseEntered(MouseEvent event) {}
- public void mouseExited(MouseEvent event) {}
- public void mousePressed(MouseEvent event) {}
- public void mouseReleased(MouseEvent event) {}
- // get user involvement in menu item
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnPlay)
- reset();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement