You mission is to create a very simple game app. The game app should allow the u
ID: 3826195 • Letter: Y
Question
You mission is to create a very simple game app. The game app should allow the user to do something (like click buttons) to play the game. You can create any game you want, but make it simple because you only have this week to work on it. You could do a guess something game, tic tax toe game, 21 card game, or whatever you choose. These instructions are generic for a reason. Be creative and have fun, but don't do something too complicated for the time you have. The game MUST LOOK PROFESSIONAL. So spend some time on the GUI layout.
Explanation / Answer
Below is a tictactoe game in Java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TicTacToe extends JPanel
{
JButton buttons[] = new JButton[9];
int alternate = 0;//if this number is a even, then put a X. If it's odd, then put an O
public TicTacToe()
{
setLayout(new GridLayout(3,3));
initializeButton();
}
public void initializeButton()
{
for(int i = 0; i <= 8; i++)
{
buttons[i] = new JButton();
buttons[i].setText("");
buttons[i].addActionListener(new buttonListener());
add(buttons[i]); //adds this button to JPanel (note: no need for JPanel.add(...)
//because this whole class is a JPanel already
}
}
public void resetButton()
{
for(int i = 0; i <= 8; i++)
{
buttons[i].setText("");
}
}
// when a button is clicked, it generates an ActionEvent.
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton buttonClicked = (JButton)e.getSource(); //get the particular button that was clicked
if(alternate%2 == 0)
buttonClicked.setText("X");
else
buttonClicked.setText("O");
if(checkWin() == true)
{
JOptionPane.showConfirmDialog(null, "Game Over.");
resetButton();
}
alternate++;
}
public boolean checkWin()
{
/** Reference: the button array is arranged like this as the board
* 0 | 1 | 2
* 3 | 4 | 5
* 6 | 7 | 8
*/
//horizontal win check
if( checkAdjacent(0,1) && checkAdjacent(1,2) ) //no need to put " == true" because the default check is for true
return true;
else if( checkAdjacent(3,4) && checkAdjacent(4,5) )
return true;
else if ( checkAdjacent(6,7) && checkAdjacent(7,8))
return true;
//vertical win check
else if ( checkAdjacent(0,3) && checkAdjacent(3,6))
return true;
else if ( checkAdjacent(1,4) && checkAdjacent(4,7))
return true;
else if ( checkAdjacent(2,5) && checkAdjacent(5,8))
return true;
//diagonal win check
else if ( checkAdjacent(0,4) && checkAdjacent(4,8))
return true;
else if ( checkAdjacent(2,4) && checkAdjacent(4,6))
return true;
else
return false;
}
public boolean checkAdjacent(int a, int b)
{
if ( buttons[a].getText().equals(buttons[b].getText()) && !buttons[a].getText().equals("") )
return true;
else
return false;
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Tic-Tac-Toe");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(new TicTacToe());
window.setBounds(300,200,300,300);
window.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.