IN JAVA PLEASE For this project you will create a GUI based

IN JAVA PLEASE

For this project, you will create a GUI based tic-tac-toe game. The game is played by two players, where one player has the X symbol and the other the O symbol. Players use a 3x3 board and play until the board is filled or a player gets a 3 square line filled (vertical, horizontal, and diagonal all work). A game is considered a tie if neither play gets a row. Tic Tac Toe File Figure 1 A win for the circle player

Solution

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.sai.game.BoardGame;
import com.sai.game.BoardGame.GameState;
import com.sai.model.BoardTwoPlayers;

public class Gametictactoe extends JPanel implements Observer{

   private static final long serialVersionUID = 6474899766649949747L;

   private int dvsonwidth = 0;
  
   private int dvsnsHgt = 0;

   BoardGame crntBrdGm;

  
   boolean dltCliks = true;
  
  
   public Gametictactoe(BoardGame brdGame){
       resetWithNewGame(brdGame);
   }
  
   public void resetWithNewGame(BoardGame brdGame){
       if (crntBrdGm != null) {
           crntBrdGm.deleteObservers();
       }
       crntBrdGm = brdGame;
       crntBrdGm.addObserver(this);
       init();
   }


  
   private void init(){
       dvsnsHgt = crntBrdGm.getBoard().getHeight();
       dvsonwidth = crntBrdGm.getBoard().getWidth();
       dltCliks = true;
       addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               if (dltCliks) {
                   mouseClickHandler(e);
               }
           }
       });
   }


   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(Color.BLACK);
       g.fillRect(0, 0, getBounds().width, getBounds().height);

       drawLines(g);
       fillWithMoves(g);
   }
   private void drawLines(Graphics g){
       Graphics2D g2 = (Graphics2D) g;
       g.setColor(Color.YELLOW);

       int fullWidth = getBounds().width;
       int fullheight = getBounds().height;

       g2.setStroke(new BasicStroke(3));
       for (int i = 1; i < dvsonwidth; i++) {
           float currentX = i * (fullWidth / dvsonwidth);
           Line2D newLine = new Line2D.Float(new Point2D.Float(currentX, 0.0f), new Point2D.Float(currentX, fullheight));
           g2.draw(newLine);  
       }

       for (int i = 1; i < dvsnsHgt; i++) {
           float currentY = i * (fullheight / dvsnsHgt);
           Line2D newLine = new Line2D.Float(new Point2D.Float(0, currentY), new Point2D.Float(fullWidth, currentY));
           g2.draw(newLine);  
       }
   }

   private void fillWithMoves(Graphics g){
       int divisionWidth = getWidth() / dvsonwidth;
       int divisionHeight = getHeight() / dvsnsHgt;
      
       int fontSize = Math.min(divisionWidth, divisionHeight) /2;
       g.setFont(new Font(\"Arial\", Font.PLAIN, fontSize));
      
      
       for (int i = 0; i < dvsonwidth; i++) {
           for (int j = 0; j < dvsnsHgt; j++) {
              
               String str = \"-\";
               int currentPosition = this.crntBrdGm.getBoard().getPiece(new Point(i,j));
               if (currentPosition == BoardTwoPlayers.PLAYER_EMPTY) {
                   str = \"-\";
               }else if (currentPosition == BoardTwoPlayers.PLAYER_O) {
                   str = \"O\";
               }else {
                   str = \"X\";
               }
              
               int currentOffsetX = (i * divisionWidth) + (divisionWidth / 2) - fontSize/2;
               int currentOffsetY = (j * divisionHeight) + (divisionHeight /2);
               g.drawString(str, currentOffsetX, currentOffsetY);
           }
       }

   }

   private void mouseClickHandler(MouseEvent event){
       if (!dltCliks) {
           return;
       }
       int width = getWidth();
       int height = getHeight();

       int cellWidth = width / dvsonwidth;
       int cellHeight = height / dvsnsHgt;

       int column = event.getX() / cellWidth;
       int row = event.getY() / cellHeight;

       Point selectedCell = new Point(column, row);
       System.out.println(\"point touched: \" + selectedCell);
      
  
       if (crntBrdGm.getBoard().isValidMove(selectedCell)){
           try {
               crntBrdGm.makeMoveForNextCurrentPlayer(selectedCell);
           } catch (Exception e) {
               e.printStackTrace();
           }
       }else{
           System.out.println(\"not a valid move. Valid moves are:\");
           System.out.println(crntBrdGm.getBoard().getEmptyValidSpaces().toString());
           System.out.println(\"\ \");
       }
      
      
   }

   public enum GameType{
       connect4, tictactoe
   }

   @Override
   public void update(Observable o, Object arg) {
       this.repaint();
      
       GameState currentState = crntBrdGm.getCurrentGameState();
       if (currentState == GameState.oWins || currentState == GameState.xWins) {
           JOptionPane.showMessageDialog(null, (currentState == GameState.oWins? \"O\" : \"X\") + \" wins the game!\");
           dltCliks = false;
       }else {
           if (crntBrdGm.getCurrentGameState() == GameState.draw) {
               JOptionPane.showMessageDialog(null, \"Game is a draw!\");
           }
       }
      
       System.out.println(crntBrdGm.getBoard());
   }
  
   public boolean gameHasStopped(){
       return !dltCliks;
   }
}

IN JAVA PLEASE For this project, you will create a GUI based tic-tac-toe game. The game is played by two players, where one player has the X symbol and the othe
IN JAVA PLEASE For this project, you will create a GUI based tic-tac-toe game. The game is played by two players, where one player has the X symbol and the othe
IN JAVA PLEASE For this project, you will create a GUI based tic-tac-toe game. The game is played by two players, where one player has the X symbol and the othe
IN JAVA PLEASE For this project, you will create a GUI based tic-tac-toe game. The game is played by two players, where one player has the X symbol and the othe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site