So I have an assignment due tomorrow and I've been trying to get this program to work, but it's quite difficult for me to do. I need help in taking the user's input and placing it on the grid and checking for all the matches to see if one of the players won.
Thank-you!
/*
* CPT program
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class CPT implements ActionListener
{
JFrame frame;
JPanel contentPane;
JButton displayMessage;
public CPT()
{
/* 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();
count = count + 1;
// Check whose turn
if (player1Turn ==1)
{
System.out.println ("B"); player2Turn=2;
}
else
{
System.out.println ("R"); player1Turn=1;
}
//Check if spot is free on board
if (grid[choice].equals ("R") || grid[choice].equals("B"))
{
System.out.println("This spot is already taken. Try again!");
choice = input.nextInt();
}
else
{
grid[choice] = "R";
}
//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);
CPT myApplication = new CPT();
}
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();
}
});
}
}