Tic-Tac-Toe game using java
You will create a very basic but functional Tic-Tac-Toe game. The first move must draw an X, the next must draw an O, and so on. You must not be able to overwrite a position that is already filled on the board. You do not need to check for game completion, the game simply ends when the board is full. I have provided a documented example which you should use as your starting point. See the comments for implementation hints and details. When a button is pressed down, the X or O should be drawn in the appropriate position, but it must not be committed until the mouse is finally released. That is, a player should be able to drag the mouse inside of the grid and the X or O should follow their cursor.
Answer:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TicTacToe1 extends JFrame { private int turns = 1; private char[][] board = new char[3][3]; private int xTable[] = {46, 79, 113}; // xTable[i] gives the displacement for rowNumber i private int yTable[] = {113, 146, 180}; private boolean moveInProgress = false; private boolean moveSuccessful; private int potentialRow; private int potentialCol; public TicTacToe1(){ super("Tictactoe Board"); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Clear board for(int r = 0; r < 3; r++) { for(int c = 0; c < 3; c++) { board[r] = '\0'; } } addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { potentialRow = getMouseRow(e); potentialCol = getMouseCol(e); moveInProgress = true; moveSuccessful = addMove(potentialRow, potentialCol); repaint(); } @Override public void mouseReleased(MouseEvent e) { if(moveInProgress) { moveInProgress = false; if(moveSuccessful) { turns++; } repaint(); } } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }); addMouseMotionListener(new MouseMotionListener() { @Override public void mouseDragged(MouseEvent e) { if(moveInProgress) { int mouseRow = getMouseRow(e); int mouseCol = getMouseCol(e); if( !(mouseRow == potentialRow && mouseCol == potentialCol) ) { if(moveSuccessful) { deleteMove(potentialRow, potentialCol); } moveSuccessful = addMove(mouseRow, mouseCol); potentialRow = mouseRow; potentialCol = mouseCol; } repaint(); } } @Override public void mouseMoved(MouseEvent e) { } }); setVisible(true); } public void paint(Graphics g){ int red = 255, green = 0, blue = 0; Font my_font; my_font = new Font( "Serif", Font.BOLD, 18 ); super.paint(g); // Note use of super!! g.setColor(new Color(red, green, blue)); // One way to set color g.drawLine(40, 140, 140, 140); g.drawLine(40, 173, 140, 173); g.drawLine(73, 107, 73, 207); g.drawLine(107, 107, 107, 207); g.setColor(new Color(0, 0, 0)); g.fillRect(20, 87, 140, 5); g.fillRect(20, 87, 5, 140); g.fillRect(20, 222, 140, 5); g.fillRect(155, 87, 5, 140); g.setColor(Color.blue); // Another way to set color g.setFont(my_font); g.drawString("My Tic Tac Toe Board", 20, 280); displayMoves(g); } private boolean deleteMove(int row, int col) { if(board[row][col] == '\0') { return false; } else { board[row][col] = '\0'; return true; } } private boolean addMove(int row, int col) { if(board[row][col] == '\0') { board[row][col] = (turns % 2 == 1) ? 'X' : 'O'; return true; } else { return false; } } private int getMouseCol(MouseEvent e) { if(e.getX() <= 73) { return 0; } else if(e.getX() <= 107) { return 1; } else { return 2; } } private int getMouseRow(MouseEvent e) { if(e.getY() <= 140) { return 0; } else if(e.getY() <= 173) { return 1; } else { return 2; } } private void displayMoves(Graphics g){ for(int r = 0; r < 3; r++) { for(int c = 0; c < 3; c++) { switch(board[r]) { case 'X': drawX(g, r, c); break; case 'O': drawCircle(g, r, c); break; } } } } private void drawX(Graphics g, int rowNo, int columnNo){ g.drawLine(xTable[columnNo], yTable[rowNo], xTable[columnNo] + 10, yTable[rowNo] + 20); g.drawLine(xTable[columnNo], yTable[rowNo] + 20, xTable[columnNo] + 10, yTable[rowNo]); } private void drawCircle(Graphics g, int rowNo, int columnNo){ g.drawOval(xTable[columnNo], yTable[rowNo], 20, 20); } public static void main(String args[]){ new TicTacToe1(); } }
Leave a reply