SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Swing I
Exercise:
Programming Projects
Question:2 | ISBN:9780132830317 | Edition: 5

Question

Design and code a Swing GUI for a two-player tic-tac-toe (noughts and crosses) game on a game board. The JFrame should use a BorderLayout with a JLabel in the NORTH region to display messages (e.g., who won the game), and a JPanel in the CENTER region to display the game board. For the game board in the JPanel , use a GridLayout manager with a layout of JButton ’s in each cell to display the game board. The button labels should initially be blank. When a player clicks on an empty button, an appropriate “X” or “O” should be placed in the label field of the button. If there is a winner (three in a row), then the program should display the winner in the Jlabel located at the top of the window. If all nine cells have been filled without a winner, the program should indicate that there is a tie.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TicTacToeGame extends JFrame implements ActionListener 
{
	private JLabel msgs;
	private JPanel board;
	private JButton[][] bdSports;
	private char[][] symbls;
	private boolean turnX;

	public TicTacToeGame() {

		super("Tic-Tac-Toe Game");
		this.setLayout(new BorderLayout());
		this.setSize(500, 500);
		msgs = new JLabel("This is now player X's turn.");
		msgs.setVisible(true);
		this.add(msgs, BorderLayout.NORTH);

		board = new JPanel(new GridLayout(3, 3));
		board.setVisible(true);
		bdSports = new JButton[3][3];
		symbls = new char[3][3];
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				bdSports[i][j] = new JButton(" ");
				bdSports[i][j].setEnabled(true);
				bdSports[i][j].addActionListener(this);
				bdSports[i][j].setActionCommand(i + " " + j);
				bdSports[i][j].setVisible(true);
				board.add(bdSports[i][j]);
				symbls[i][j] = '.';
			}
		}
		this.add(board, BorderLayout.CENTER);
		turnX = true;
	}

	private boolean won(char playerMark, char[][] board)
	{
		for (int row = 0; row < 3; row++) 
		{
			int count = 0;
			for (int col = 0; col < 3; col++) 
			{
				if (board[row][col] == playerMark)
					count++;
				else
					count = 0;
			}

			if (count == 3)
				return true;
		}

		for (int col = 0; col < 3; col++)
		{
			int count = 0;
			for (int row = 0; row < 3; row++)
			{
				if (board[row][col] == playerMark)
					count++;
				else
					count = 0;
			}

			if (count == 3)
				return true;
		}
		if (board[1][1] == playerMark && board[0][0] == playerMark && board[2][2] == playerMark)
			return true;

		if (board[1][1] == playerMark && board[0][2] == playerMark && board[2][0] == playerMark)
			return true;
		return false;
	}

	private boolean tie(char[][] board) 
	{

		for (int i = 0; i < 3; i++) 
		{
			for (int j = 0; j < 3; j++) 
			{
				if (board[i][j] == '.')
					return false;
			}
		}

		return true;
	}

	public void actionPerformed(ActionEvent e) 
	{

		String s = e.getActionCommand();
		if (s.length() == 3) {

			Scanner input = new Scanner(s);
			String rowString = input.next();
			String colString = input.next();

			int row = Integer.parseInt(rowString);
			int col = Integer.parseInt(colString);

			JButton button = bdSports[row][col];

			if (button.isEnabled()) {
				if (turnX) {
					msgs.setText("This is now player O's turn.");
					button.setText("X");
					symbls[row][col] = 'X';
					button.setEnabled(false);
					turnX = false;
				} else {
					msgs.setText("This is now player X's turn.");
					button.setText("O");
					symbls[row][col] = 'O';
					button.setEnabled(false);
					turnX = true;
				}
			}
			if (tie(symbls))
			{
				msgs.setText("Tie Game! ");
				for (int i = 0; i < 3; i++)
					for (int j = 0; j < 3; j++)
						bdSports[i][j].setEnabled(false);
			}
			if (won('X', symbls))
			{
				msgs.setText("Player X has won!");
				for (int i = 0; i < 3; i++)
					for (int j = 0; j < 3; j++)
						bdSports[i][j].setEnabled(false);
			}

			if (won('O', symbls))
			{
				msgs.setText("Player O has won!");
				for (int i = 0; i < 3; i++)
					for (int j = 0; j < 3; j++)
						bdSports[i][j].setEnabled(false);
			}
		}
	}

	public static void main(String args[])
	{
		TicTacToeGame tictac = new TicTacToeGame();
		tictac.setVisible(true);
	}
}

OUTPUT:

 

0 0

Discussions

Post the discussion to improve the above solution.