Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main.java
- import javax.swing.JFrame; // for creating windows
- public class Main {
- public static void main(String[] args) {
- JFrame window= new JFrame();
- window.setSize(800, 600);
- window.setTitle("Tic-tac-toe");
- window.setVisible(true);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Game game=new Game();
- //Board board= new Board();
- window.add(game);
- }
- }
- //Board.java (basically a trimmed down version from before separating the game from the graph
- import javax.swing.*;
- import java.awt.*; // wildcard first level only
- import java.awt.geom.*; // drawing
- //import java.awt.event.*;
- public class Board extends JComponent{
- /* method paintConponent
- * inherited from JComponent
- * non-Javadoc)
- * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
- */
- public void paintComponent(Graphics g)
- {
- Graphics2D g2= (Graphics2D) g;
- // create background
- g2.setColor(Color.BLUE);
- g2.fillRect(0, 0, this.getWidth(), this.getHeight());
- g2.setColor(Color.WHITE);
- // first vertical line of board
- Line2D.Double v1=new Line2D.Double(350, 150, 350, 450);
- g2.draw(v1);
- // second vertical line of board
- Line2D.Double v2=new Line2D.Double(450, 150, 450, 450);
- g2.draw(v2);
- // first horizontal line of board
- Line2D.Double h1=new Line2D.Double(250, 250, 550, 250);
- g2.draw(h1);
- // second horizontal line of board
- Line2D.Double h2=new Line2D.Double(250, 350, 550, 350);
- g2.draw(h2);
- // creating the content for the 9 cells;
- Font text= new Font("serif", Font.BOLD, 72);
- g2.setFont(text);
- for (int i=0; i<9; i++)
- {
- // type-casting required as drawstring required ints while x and y in cells are doubles
- g2.drawString(Game.cells[i].getContent(), (int)Game.cells[i].getx(), (int)Game.cells[i].gety());
- }
- // change font for bottom text
- Font textstatus= new Font("serif", Font.PLAIN, 16);
- g2.setFont(textstatus);
- g2.drawString(Game.status, 350, 550);
- }
- }
- //Game.java (contains all the game info from the old Board.java)
- // displays key information about game
- public class Game extends Board{
- public static String move="X";
- public static String status="Player X's move";
- // the board contains the 9 cells which need drawn
- public static Cell[] cells = new Cell[9];
- public static int state=0;
- public Game()
- {
- // create the cells here
- int x,y,xlower,ylower,xupper,yupper,id;
- for (int i=0; i<3; i++)
- {
- for (int j=0; j<3; j++)
- {
- x=274+100*i;
- y=174+100*j;
- xlower=250+100*i;
- ylower=150+100*j;
- xupper=350+100*i;
- yupper=250+100*j;
- id=3*i+j;
- Game.cells[id]=new Cell(x,y,xlower,ylower,xupper,yupper,id,"");
- addMouseListener(Game.cells[id]);
- }
- }
- }
- public void GameState()
- {
- // standard iterator
- int i=0;
- // WinCombos is a 2D array showing the 8 possible ways of winning
- 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}};
- // iterate through the above array
- for (i=0; i<8; i++)
- {
- // these 2 if statements should be combined, but this way leads to shorter lines
- if (Game.cells[WinCombos[i][0]].getContent()==Game.cells[WinCombos[i][1]].getContent())
- {
- if (Game.cells[WinCombos[i][0]].getContent()==Game.cells[WinCombos[i][2]].getContent())
- {
- // if it's got in here, then one of the winning combos has happened, check who won
- if (Game.cells[WinCombos[i][0]].getContent()=="X")
- {
- Game.status="Player X wins";
- Game.state=1;
- this.repaint();
- return;
- }
- if (Game.cells[WinCombos[i][0]].getContent()=="O")
- {
- Game.status="Player O wins";
- Game.state=2;
- this.repaint();
- return;
- }
- }
- }
- }
- // if no-one has won, is the game still ongoing?
- for (i=0; i<9; i++)
- {
- if (Game.cells[i].getContent()=="")
- {
- Game.status="Player "+Game.move+"'s turn";
- Game.state=0; // game still in progress
- this.repaint();
- return;
- }
- }
- Game.status="Draw";
- Game.state=3;
- this.repaint();
- return; // game drawn
- }
- }
- //Cell.java
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.event.MouseInputListener;
- public class Cell implements MouseListener, MouseInputListener{
- private String content;
- private double x;
- private double y;
- private double xlower;
- private double ylower;
- private double xupper;
- private double yupper;
- public int id;
- /* constructor Cell:
- * parameters:-
- * x: x co-ordinate for the text
- * y: y-co-ordinate for the text
- * xlower: the lower x bound for the cell
- * ylower: the lower y bound for the cell
- * xupper: the upper x bound for the cell
- * yupper: the upper y bound for the cell
- * text: what the cell should display
- *
- */
- public Cell(double x, double y, double xlower, double ylower, double xupper, double yupper, int id, String text)
- {
- this.x=x;
- this.y=y;
- this.xlower=xlower;
- this.ylower=ylower;
- this.xupper=xupper;
- this.yupper=yupper;
- this.id=id;
- this.content=text;
- }
- public void setx(double x)
- {
- this.x=x;
- }
- public double getx()
- {
- return this.x;
- }
- public void sety(double y)
- {
- this.y=y;
- }
- public double gety()
- {
- return this.y;
- }
- public void setContent(String text)
- {
- this.content=text;
- }
- public String getContent()
- {
- return this.content;
- }
- // upper and lower bounds are never changed hence no setter function
- public double getxlower()
- {
- return this.xlower;
- }
- public double getylower()
- {
- return this.ylower;
- }
- public double getxupper()
- {
- return this.xupper;
- }
- public double getyupper()
- {
- return this.yupper;
- }
- @Override
- public void mouseDragged(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseMoved(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.println("Cell "+this.id+" clicked");
- if (this.getContent()!="")
- {
- this.setContent(Game.move);
- if (Game.move=="X")
- {
- Game.move="O";
- }
- else
- {
- Game.move="X";
- }
- Game.cells[this.id].setContent(Game.move);
- }
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement