I need help in my Connect Four program. Specifically in placing the user's input on the gameboard & also checking horizonallty, vertically, etc.
/*
* ConnectFour program
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class ConnectFour implements ActionListener
{
JFrame frame;
JPanel contentPane;
JButton displayMessage;
public ConnectFour()
{
/* Create and set up the frame */
frame = new JFrame("Connect Four");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Create a content pane with a BoxLayout and empty borders */
contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.setBackground(Color.white);
contentPane.setLayout(new GridLayout(0, 2, 5, 10));
/* Create a button */
displayMessage = new JButton("Play Now!");
displayMessage.setBackground(Color.yellow);
displayMessage.setForeground(Color.blue);
displayMessage.addActionListener(this);
contentPane.add(displayMessage);
/* Add content pane to frame */
frame.setContentPane(contentPane);
/* Size and then display the frame. */
frame.pack();
frame.setVisible(true);
}
/**
* Handle a the button click
* pre: none
* post: A message has been displayed.
*/
public void actionPerformed(ActionEvent event)
{
Scanner input = new Scanner(System.in);
String player1;
String player2;
int choice;
int currentPlayer;
int player1Turn = 1;
int player2Turn = 2;
int draw = 0;
int win = 0;
int count =0;
//Columns
int col1 = 12;
int col2 = 13;
int col3 = 14;
int col4 = 15;
//Prompt Player for Name
System.out.print("Player one, enter your name:");
player1 = input.nextLine();
player1 = player1.toUpperCase();
System.out.print("Player two, enter your name:");
player2 = input.nextLine();
player2 = player2.toUpperCase();
//Assign Players a Color (Black or Red)
System.out.println(player1 + " " + "you are BLACK." + " " + player2 + " " +"you are RED");
System.out.println("");
do
{
//Display Grid
String [] grid = new String [16];
System.out.println("1 2 3 4");
for (int i = 0; i<grid.length; i++)
{
grid[i]="*";
}
for (int i = 0; i <grid.length; i++)
{
System.out.print (grid[i]+ " ");
if(i%4 == 3)
{
System.out.println (" ");
}
}
//Ask user what spot they want to play their chip
System.out.println(player1 + " , which column do you want to place your chip in?");
choice = input.nextInt();
//Check if spot is free on board
if (grid[choice].equals ("B") || grid[choice].equals("R"))
{
System.out.println("This spot is already taken. Try again!");
choice = input.nextInt();
}
else
{
grid[choice] ="B";
}
//Counter
count = count + 1;
// Check whose turn
if (player1Turn ==1)
{
System.out.println ("B"); player2Turn=2;
}
else
{
System.out.println ("R"); player1Turn=1;
}
//Check if tie game
if (count == 16)
{
System.out.print("Tie Game.");
draw = 1;
}
}while (draw==0 && win==0);
}
/**
* Create and show the GUI.
*/
private static void runGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
ConnectFour myApplication = new ConnectFour();
}
public static void main(String[] args)
{
/* Methods that create and show a GUI should be
run from an event-dispatching thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGUI();
}
});
}
}