java.lang.reflect.invocationtargetexception error when placing applet onto website - java

I'm having trouble getting my applet to run on my website. It's my first time embedding an applet to a website and I have no idea what is going wrong.
I created the .jar file following the following website's instructions:https://eyeasme.com/Shayne/HTML5_APPLETS/
And here is the code for the java applet.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class TicTacToe extends JFrame
{
private JButton button [][] = new JButton [3][1];
private boolean checkerO [][] = new boolean [3][2];
private boolean checkerX [][] = new boolean [3][3];
private JPanel panel;
private final int WINDOW_WIDTH = 200;
private final int WINDOW_HEIGHT = 200;
private int turn = 1;
public TicTacToe()
{
setTitle ("Tic-Tac-Toe");
setSize (WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and register an event listener with all buttons
for(int i = 0; i <= 2; i++)
{
for(int j = 0; j <= 2; j++)
{
button [i][j] = new JButton();
button [i][j].addActionListener(new ButtonListener());
button [i][j].setFont(new Font("Arial", Font.PLAIN, 35));
}
}
for(int i = 0; i <= 2; i++)
{
for(int j = 0; j <= 2; j++)
{
checkerO [i][j] = false;
}
}
for(int i = 0; i <= 2; i++)
{
for(int j = 0; j <= 2; j++)
{
checkerX [i][j] = false;
}
}
//create a panel, work on the layout and add the buttons
panel = new JPanel();
GridLayout myLayout = new GridLayout(3,3);
panel.setLayout(myLayout);
for (int i = 0; i <= 2; i++)
{
for(int j = 0; j <= 2; j++)
{
panel.add(button[i][j]);
}
}
//add panel to content pane
add(panel);
//display window
setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//determine which button is clicked
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if (e.getSource() == button[i][j])
{
//Check if the chosen button has already been picked.
if (checkerX[i][j] || checkerO[i][j])
{
JOptionPane.showMessageDialog(null, "I'm sorry, Dave. I'm afraid I can't do that.");
}
else if (turn == 1 || turn == 3 || turn == 5 || turn == 7 || turn == 9)
{
button[i][j].setText("X");
checkerX[i][j] = true;
turn++;
}
//Checks all possible combinations for X to see if X won
if ((checkerX[0][0] == true && checkerX[0][4] == true && checkerX[0][5] == true) || (checkerX[1][0] == true && checkerX[1][6] == true && checkerX[1][7] == true) || (checkerX[2][0] == true && checkerX[2][8] == true && checkerX[2][9] == true) || (checkerX[0][0] == true && checkerX[1][0] == true && checkerX[2][0] == true) || (checkerX[0][10] == true && checkerX[1][11] == true && checkerX[2][12] == true) || (checkerX[0][13] == true && checkerX[1][14] == true && checkerX[2][15] == true) || (checkerX[0][0] == true && checkerX[1][16] == true && checkerX[2][17] == true) || (checkerX[0][18] == true && checkerX[1][19] == true && checkerX[2][0] == true))
{
JOptionPane.showMessageDialog(null, "X wins the game.");
System.exit(0);
}
//If X hasn't won, run the AI
runAI();
//If turn goes past 9, it means it's a tie
if (turn == 10)
{
JOptionPane.showMessageDialog(null, "It's a tie.");
System.exit(0);
}
}
}
}
}
public void runAI()
{
Random generator = new Random();
boolean picked = false;
if (turn == 2 || turn == 4 || turn == 6 || turn == 8)
{
while (picked == false)
{
int position1 = generator.nextInt(3);
int position2 = generator.nextInt(3);
//If the position is already picked, the AI will roll a random number again.
if (checkerX[position1][position2] || checkerO[position1][position2])
{
}
//If the position is empty, put an O.
else
{
button[position1][position2].setText("O");
checkerO[position1][position2] = true;
turn++;
picked = true;
}
}
}
//Checks all possible combinations for O to see if O won
if ((checkerO[0][0] == true && checkerO[0][20] == true && checkerO[0][21] == true) || (checkerO[1][0] == true && checkerO[1][22] == true && checkerO[1][23] == true) || (checkerO[2][0] == true && checkerO[2][24] == true && checkerO[2][25] == true) || (checkerO[0][0] == true && checkerO[1][0] == true && checkerO[2][0] == true) || (checkerO[0][26] == true && checkerO[1][27] == true && checkerO[2][28] == true) || (checkerO[0][29] == true && checkerO[1][30] == true && checkerO[2][31] == true) || (checkerO[0][0] == true && checkerO[1][32] == true && checkerO[2][33] == true) || (checkerO[0][34] == true && checkerO[1][35] == true && checkerO[2][0] == true))
{
JOptionPane.showMessageDialog(null, "O wins the game.");
System.exit(0);
}
}
}
public static void main (String [] args)
{
TicTacToe ttt = new TicTacToe();
}
}
There was a bit of a formatting problem when I tried to paste this on here so there might be some typos in the code. But the program actually runs as a .exe file.
Here's the HTML code:
<html>
<head>
<title>Minigames for All</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h2 id="header">Welcome to Minigames for All.</h2>
<hr>
<object type="application/x-java-applet" height="300" width="550">
<param name="code" value="TicTacToe" />
<param name="archive" value="applet/TicTacToe.jar" />
Applet failed to run. No Java plug-in was found.
</object>
<hr>
<table>
<tbody>
<tr>
<td><a href="rps.html">Rock, Paper, Scissors</td>
<td><a href="random.html">Guess the Number</td>
<td><a href="ttt.html">Tic Tac Toe</td>
</tr>
<tr>
<td><a href="flip.html">Flip a Coin</td>
<td><a href="rpg.html">Slime RPG</td>
<td><a href="shoot.html">Space Shooter</td>
</tr>
<tr>
<td>&nbsp</td>
<td><a href="index.html">Home Page</td>
<td>&nbsp</td>
</tr>
</tbody>
</table>
</body>
</html>
Manifest file:
Manifest-Version: 1.0
Created-By: 1.7.0_21 (Oracle Corporation)
Error Message:http://puu.sh/5J4z1.png

The first thing I notice is this
TicTacToe extends JFrame
not
TicTacToe extends JApplet // or even Applet.
Are you sure you have an Applet?

Related

tic tac toe java user vs computer

I am writing a tic tac toe game for my class. Everything is working but I am unable to figure out how to make my computer player choose only spaces that are available. My code is glitching and allowing the computer to choose either the other players spaces or not playing at all. Any help will be appreciated.
import java.util.Random;
import java.util.Scanner;
public class TicTacToe1 {
public static void main(String[] args) {
welcome();
initializeBoard();
printBoard();
while ((!checkWin()) && (!checkDraw())) {
playerMove();
printBoard();
System.out.println();
computerMove();
printBoard();
}
System.out.println();
if (checkWin() == true) {
System.out.println("The winner is " + currentTurn);
}
if (checkDraw() == true) {
System.out.println("Draw");
}
}
private static String[][] board = new String[3][3];
private static int row, column;
public static Scanner scan = new Scanner(System.in);
public static String currentTurn = "X";
// public static String computerTurn = "O";
public static String turn() {
if (currentTurn == "X") {
currentTurn = "O";
} else {
currentTurn = "X";
}
return currentTurn;
}
private static void welcome() {
System.out.println("Tic Tac Toe");
System.out.println("Please enter your coordinates for your location row (1-3) column (1-3):");
}
public static void initializeBoard() { // initialize tic tac toe
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j] = "-";
}
}
}
public static void printBoard() {
for (int i = 0; i < board.length; i++) {
System.out.println();
for (int j = 0; j < board.length; j++) {
if (j == 0) {
System.out.print("| ");
}
System.out.print(board[i][j] + " | ");
}
}
}
public static void playerMove() {
System.out.println();
System.out.println("Your Move: ");
row = scan.nextInt() - 1;
column = scan.nextInt() - 1;
if (board[row][column] == "-") {
board[row][column] = turn();
} else {
System.out.println("Invalid entry. Please go again");
row = scan.nextInt() - 1;
column = scan.nextInt() - 1;
board[row][column] = turn();
}
}
// public static void computerMove() {
// Random computerMove = new Random();
// row = computerMove.nextInt(3);
// column = computerMove.nextInt(3);
// if (board[row][column] == "-") {
// board[row][column] = turn();
// } else {
// }
// }
public static void computerMove() {
Random computerMove = new Random();
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
while (board[row][column] != "-") {
// Random computerMove = new Random();
// row = computerMove.nextInt(3);
// column = computerMove.nextInt(3);
if (board[row][column] == "-") {
board[row][column] = turn();
} else {
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
board[row][column] = turn();
}
}
}
public static boolean checkWin() {
return (checkDiagonalWin() || checkHorizontalWin() || checkVerticalWin());
}
public static boolean checkDiagonalWin() {
if ((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[1][1] != "-")) {
return true;
}
if ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[1][1] != "-")) {
return true;
}
return false;
}
public static boolean checkHorizontalWin() {
// for (int i = 0; i < board.length; i++) {
if ((board[0][0] == board[0][1]) && (board[0][0] == board[0][2]) && (board[0][0] != "-")) {
return true;
}
if ((board[1][0] == board[1][1]) && (board[1][0] == board[1][2]) && (board[1][0] != "-")) {
return true;
}
if ((board[2][0] == board[2][1]) && (board[2][0] == board[2][2]) && (board[2][0] != "-")) {
return true;
}
// }
return false;
}
public static boolean checkVerticalWin() {
// for (int j = 0; j < board.length; j++) {
if ((board[0][0] == board[1][0]) && (board[0][0] == board[2][0]) && (board[0][0] != "-")) {
return true;
}
if ((board[0][1] == board[1][1]) && (board[0][1] == board[2][1]) && (board[0][1] != "-")) {
return true;
}
if ((board[0][2] == board[1][2]) && (board[0][2] == board[2][2]) && (board[0][2] != "-")) {
return true;
}
// }
return false;
}
public static boolean checkDraw() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] == "-") {
return false;
}
}
}
return true;
}
}
The issue was in your computerMove logic.
public static void computerMove() {
Random computerMove = new Random();
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
while (board[row][column] != "-") {
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
}
board[row][column] = turn();
}
This should work for you, just copy paste this in place of your computerMove.
Now as to why your code didn't work:-
Your code:
while (board[row][column] != "-") {
if (board[row][column] == "-") {
board[row][column] = turn();
} else {
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
board[row][column] = turn();
}
}
The while loop looks at the position and sees that there is no '-', thus runs. Then inside your while loop you have a if statement which checks to see whether you have '-' at that position. That can never be true, because our while loop wouldn't run otherwise.
The best idea is to let your code keep changing the row and columns until you get a position with '-', and use your while loop to do that. As soon as you get the '-', your while loop won't run anymore anyways, so you can just set the board[row][columns] = turn() just outside the while loop, and your code will work fine.
P.S. Took a lot of willpower to not make a machines are uprising reference to your
My code is glitching and allowing the computer to choose either the other players spaces or not playing at all
Have fun with your program :)
~HelpfulStackoverflowCommunity

make players change after certain time in java

I want to add the following functionality in a tictactoe game: if a player is on turn but he/she doesn't do anything for a certain time (10 seconds), than it's the another player's turn.
In the "GameHub" class (extends a Server class for creating only one game) I have the inner class "GameState", which maintains the current state of the game and passes it as a message to the server (and then it is forwarded to all clients/players).
public class GameHub extends Server {
private GameState state;
public GameHub(int port) throws IOException {
super(port);
state = new GameState();
setAutoreset(true);
}
protected void messageReceived(int playerID, Object message) {
state.applyMessage(playerID, message);
sendToAll(state);
}
protected void playerConnected(int playerID) {
if (getPlayerList().length == 2) {
shutdownServerSocket();
state.startFirstGame();
sendToAll(state);
}
}
protected void playerDisconnected(int playerID) {
state.playerDisconnected = true;
sendToAll(state);
}
public static class GameState implements Serializable {
public boolean playerDisconnected;
public char[][] board;
public boolean gameInProgress;
public int playerPlayingX;
public int playerPlayingO;
public int currentPlayer;
public boolean gameEndedInTie;
public int winner;
public void applyMessage(int sender, Object message) {
if (gameInProgress && message instanceof int[] && sender == currentPlayer) {
int[] move = (int[]) message;
if (move == null || move.length != 2) {
return;
}
int row = move[0];
int col = move[1];
if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') {
return;
}
board[row][col] = (currentPlayer == playerPlayingX) ? 'X' : 'O';
if (winner()) {
gameInProgress = false;
winner = currentPlayer;
} else if (tie()) {
gameInProgress = false;
gameEndedInTie = true;
}
else {
currentPlayer = (currentPlayer == playerPlayingX) ? playerPlayingO : playerPlayingX;
}
} else if (!gameInProgress && message.equals("newgame")) {
startGame();
}
}
void startFirstGame() {
startGame();
}
private void startGame() {
board = new char[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
int xPlr = (Math.random() < 0.5) ? 1 : 2;
playerPlayingX = xPlr; // Will be 1 or 2.
playerPlayingO = 3 - xPlr; // The other player ( 3 - 1 = 2, and 3 - 2 = 1 )
currentPlayer = playerPlayingX;
gameEndedInTie = false;
winner = -1;
gameInProgress = true;
}
private boolean winner() {
if (board[0][0] != ' '
&& (board[0][0] == board[1][1] && board[1][1] == board[2][2])) {
return true;
}
if (board[0][2] != ' '
&& (board[0][2] == board[1][1] && board[1][1] == board[2][0])) {
return true;
}
for (int row = 0; row < 3; row++) {
if (board[row][0] != ' '
&& (board[row][0] == board[row][1] && board[row][1] == board[row][2])) {
return true;
}
}
for (int col = 0; col < 3; col++) {
if (board[0][col] != ' '
&& (board[0][col] == board[1][col] && board[1][col] == board[2][col])) {
return true;
}
}
return false;
}
private boolean tie() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
return false;
}
}
}
return true;
}
}
}
For the time measurement I have the "Countdown" class, which aim is to change the players after the required time elapsed.
public class Countdown {
int timer;
public void counter(int timeFrame) {
timer = timeFrame;
Timer TimerA = new Timer();
TimerTask TaskA = new TimerTask() {
#Override
public void run() {
if (timer >= 0) {
timer--;
}
if (timer == -1) {
currentPlayer = (currentPlayer == playerPlayingX) ? playerPlayingO : playerPlayingX;
TimerA.cancel();
}
}
};
TimerA.schedule(TaskA, 0, 1000);
}
public int getTimer(){
return timer;
}
}
Exactly at that part I'm stuck. In my opinion I need to add and start the timer somewhere in the "GameState" class, but for some reason I can't figure it out where exactly.
int timeFrame = 10;
Countdown C = new Countdown();
C.counter(timeFrame);
I thought it should be started in that "else block"
else {currentPlayer = (currentPlayer == playerPlayingX) ? playerPlayingO : playerPlayingX;
int timeFrame = 10;
Countdown C = new Countdown();
C.counter(timeFrame);}
But it doesn't work properly => it works just for "playerPlayingO" (if he delays 10 seconds, he misses his turn). playerPlayingX is not affected...
May be I'm also missing something else...
If you're using a JavaFX you may use a task for this - Just make the task run something like this:
Thread.sleep(10000); //Wait 10 Secs
if (activePlayer == initialActivePlayer) switchPlayer(); //Better write your own "ShouldWeSwitch"-Condition
You may need to fiddle around with synchronization a little and maybe terminate the tasks when the players trigger the switch but this may work for your game.
PS: If you're not using JavaFX you can simply make your own Task by creating a class that extends Thread

How do I implement a reset button for my GUI?

I am an AP Computer Science student and I need help with my assignment for the class. My assignment is to create a simple GUI or game using Eclipse. I made a simple player vs. player tic-tac-toe game, but I do not know how to create a "reset" button for my GUI. I tried multiple times, but I can't get it to work or show up in my GUI. I would appreciate some pointers on how to implement a functional reset button, so I would not have exit out of my GUI multiple times to start playing again. Here is the code I have written so far.
package gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToeGUI implements ActionListener
{
JFrame window = new JFrame("Tic-Tac-Toe");
JButton[] button;
JButton reset = new JButton("Reset");
String letter = "";
public int count = 0;
public boolean win = false;
public TicTacToeGUI()
{
button = new JButton[9];
window.setSize(300,300);
window.setLayout(new GridLayout(3,3));
JButton dummy = new JButton("");
Font font = dummy.getFont();
Font bigFont = font.deriveFont(font.getSize2D() * 5.0f);
JButton reset = new JButton("Reset");
for(int i = 0; i < 9; i++)
{
button[i] = new JButton("");
button[i].setFont(bigFont);
button[i].addActionListener(this);
window.add(button[i]);
}
window.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
count++;
if(count % 2 == 1)
{
letter = "X";
}
else
{
letter = "O";
}
Object but = a.getSource();
for(int i = 0; i < 9; i++)
{
if(but == button[i])
{
button[i].setText(letter);
button[i].setEnabled(false);
break;
}
}
if( button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != "")
{
win = true;
}
else if(button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != "")
{
win = true;
}
else if(button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != "")
{
win = true;
}
else if(button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != "")
{
win = true;
}
else if(button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != "")
{
win = true;
}
else if(button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != "")
{
win = true;
}
else if(button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != "")
{
win = true;
}
else if(button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != "")
{
win = true;
}
else
{
win = false;
}
if(win == true)
{
JOptionPane.showMessageDialog(null, letter + " WINS!");
}
else if(count == 9 && win == false)
{
JOptionPane.showMessageDialog(null, "Tie Game!");
}
}
public static void main(String[] args)
{
new TicTacToeGUI();
}
}
You might want to try this:
window.setLayout(new BorderLayout());
JPanel panel = new JPanel( new GridLayout(3, 3));
window.add(panel, BorderLayout.CENTER); // add panel to window center
window.add(reset, BorderLayout.SOUTH); // add reset button to window bottom
Of course, you will have to add your 9 buttons to panel now, not to window.
But why don't you just reset automatically after the user confirmed the dialog at the end of the game?
Reset your board after a tie or win. An example reset method. Otherwise you are going to have to make room on your Frame to hold a button to do this.
private void ResetBoard() {
for(int i = 0; i < 9; i++) {
button[i].setText("");
button[i].setEnabled(true);
count = 0;
}
}
Then use this method when a check is made to see if a player won or the game ends in a tie as below:
if(win == true)
{
JOptionPane.showMessageDialog(null, letter + " WINS!");
ResetBoard();
}
else if(count == 9 && win == false)
{
JOptionPane.showMessageDialog(null, "Tie Game!");
ResetBoard();
}

Java connect four victory check

I am a beginner at programming and I have been teaching myself as much as I can. I need help in a simpler way of checking for a victory instead of hard coding every possible combination.
I have no idea what to do.
here is my current code:
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class connectfour extends JFrame implements ActionListener
{
JLabel board[][] = new JLabel[8][7];
//JLabel board[] = new JLabel[64];
JButton action[] = new JButton[8];
JButton start;
JButton clear;
JFrame Frame = new JFrame();
ImageIcon red = new ImageIcon("red piece.jpeg");
ImageIcon black = new ImageIcon("blackpiece.jpeg");
boolean players = true;
//Integer[] numclick = new Integer[8];
int x;
int numclick1 = 7;
int numclick2 = 7;
int numclick3 = 7;
int numclick4 = 7;
int numclick5 = 7;
int numclick6 = 7;
int numclick7 = 7;
int numclick0 = 7;
public connectfour()
{
Frame.setSize(100,120);
Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
Frame.setLayout(null);
Frame.setVisible(true);
start = new JButton("Start");
start.setBounds(0,0,100,100);
start.setVisible(true);
start.addActionListener(this);
Frame.add(start);
}
public void game()
{
for(int x = 0; x < 8 ; x++)
{
action[x] = new JButton();
action[x].setSize(100,40);
action[x].setLocation((x*100) + 50, 0);
action[x].addActionListener(this);
action[x].setVisible(true);
Frame.add(action[x]);
}
/**
board[1][1] = new JLabel();
board[1][1].setBounds(50,40,100,100);
board[1][1].setVisible(true);
board[1][1].setOpaque(true);
board[1][1].setBorder(BorderFactory.createLineBorder(Color.black));
Frame.add(board[1][1]);
**/
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 7; j++)
{
board[i][j] = new JLabel();
board[i][j].setSize(100,100);
board[i][j].setLocation((i*100)+50,(j*100)+40);
board[i][j].setOpaque(true);
board[i][j].setVisible(true);
board[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
Frame.add(board[i][j]);
}
}
clear = new JButton("Clear");
clear.setBounds(850,100,100,50);
clear.addActionListener(this);
clear.setVisible(true);
Frame.add(clear);
}
public void boardsize()
{
Frame.setSize(950,800);
}
public static void main(String args[])
{
new connectfour();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == start)
{
boardsize();
start.setVisible(false);
game();
}
for(x = 0;x < 8 ;x ++)
{
if(e.getSource() == action[x])
{
//numclick[x]++;
if(x == 0)
{
numclick0--;
if(players == true)
{
board[x][numclick0].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick0].setIcon(black);
players = true;
break;
}
}
if(x == 1)
{
numclick1--;
if(players == true)
{
board[x][numclick1].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick1].setIcon(black);
players = true;
break;
}
}
if(x == 2)
{
numclick2--;
if(players == true)
{
board[x][numclick2].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick2].setIcon(black);
players = true;
break;
}
}
if(x == 3)
{
numclick3--;
if(players == true)
{
board[x][numclick3].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick3].setIcon(black);
players = true;
break;
}
}
if(x == 4)
{
numclick4--;
if(players == true)
{
board[x][numclick4].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick4].setIcon(black);
players = true;
break;
}
}
if(x == 5)
{
numclick5--;
if(players == true)
{
board[x][numclick5].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick5].setIcon(black);
players = true;
break;
}
}
if(x == 6)
{
numclick6--;
if(players == true)
{
board[x][numclick6].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick6].setIcon(black);
players = true;
break;
}
}
if(x == 7)
{
numclick7--;
if(players == true)
{
board[x][numclick7].setIcon(red);
players = false;
break;
}
if(players == false)
{
board[x][numclick7].setIcon(black);
players = true;
break;
}
}
System.out.println(x);
System.out.println();
}
}
if(e.getSource() == clear)
{
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 7; y++)
{
board[x][y].setIcon(null);
numclick1 = 7;
numclick2 = 7;
numclick3 = 7;
numclick4 = 7;
numclick5 = 7;
numclick6 = 7;
numclick7 = 7;
numclick0 = 7;
players = true;
for(int j = 0; j < 8 ; j++)
{
action[j].setEnabled(true);
}
}
}
}
if(numclick0 == 0)
{
action[0].setEnabled(false);
}
if(numclick1 == 0)
{
action[1].setEnabled(false);
}
if(numclick2 == 0)
{
action[2].setEnabled(false);
}
if(numclick3 == 0)
{
action[3].setEnabled(false);
}
if(numclick4 == 0)
{
action[4].setEnabled(false);
}
if(numclick5 == 0)
{
action[5].setEnabled(false);
}
if(numclick6 == 0)
{
action[6].setEnabled(false);
}
if(numclick7 == 0)
{
action[7].setEnabled(false);
}
}
public void winner()
{
}
}
I would use a recursive method that checks the horizontal, vertical, and both diagonals.
As i was reading your code I realized you don't keep track(may have missed it) of where players are.. I recommend and array for this called grid[][] Mapping an array to your JLabels will go a long way.
Ill give an example of negative vertical check..
public Boolean checkVertical(Boolean player, int x, int y){
if(solveHelper(player, x, y, -1, 0) => 4) return true;
return false;
}
public int solveHelper(Boolean player, int x, int y, int addX, int addY){
if(x == 0 || x == size || y == 0 || y == size || grid[x][y].player != player)
return 0;
return solverHelper(player, x+addX, y+addY, addX, addY) + 1);
}
Now how can you create and use these methods for yourself?
you need to create a new methods for each of the horizontal, vertical, and both diagonals to check for all of them you call solveHelper with different properties in addX and addY that correspond with the direction you want to go. For instance, if you want to check the horizontal you need do make addY == 1 and addY == -1 with both values for addX == 0 by doing a solveHelper + solverHelper with these two values changed.
Other notes...
Some things you need to need to keep in mind is that how connect four actually runs. When you click on a row a piece falls down to the smallest unoccupied element in that particular column. Just something you should keep in mind when writing your game logic.
Cheers.

java, "enter program argument" error?

I'm fairly new to the java programming. Below i have my program and the error that keeps appearing. I do not know what it means, if someone could please help me. i am using crimson editor, my program compiles fine just when a window asking me "enter program arguments" that this error appears.If someone could please explain to me what it means. Thank you.
// my program
package test.rim.bbapps.testcase.lib;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class michaeltictactoe2 implements ActionListener {
/* Instance variables */
private JFrame window = new JFrame (" TicTacToe");
private JButton button1 = new JButton ("") ;
private JButton button2 = new JButton ("") ;
private JButton button3 = new JButton ("") ;
private JButton button4 = new JButton ("") ;
private JButton button5 = new JButton ("") ;
private JButton button6 = new JButton ("") ;
private JButton button7 = new JButton ("") ;
private JButton button8 = new JButton ("") ;
private JButton button9 = new JButton ("") ;
private String letter = "";
private int count = 0;
private boolean win = false;
public michaeltictactoe2 () {
//* Create Window * /
window.setSize (300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout ( new GridLayout (3, 3));
/* Adding buttons to the window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);
/* Add the action listener to the Button */
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
//* make the window visible * /
window.setVisible (true) ;
}
public void actionPerformed (ActionEvent a) {
count++;
/* Calculate who's turn it is */
if (count == 1 || count == 3|| count == 5 || count == 7 || count == 9) {
letter = "X";
} else if ( count == 2 || count == 4 || count == 6 || count == 8 ) {
letter = "O";
}
/* Display X's or O's on the buttons */
if ( a.getSource () == button1) {
button1.setText ( letter ) ;
button1.setEnabled (false);
} else if (a.getSource () == button2) {
button2.setText(letter);
button2.setEnabled(false);
} else if (a.getSource () == button3) {
button3.setText(letter);
button3.setEnabled(false);
} else if (a.getSource () == button4) {
button4.setText(letter);
button4.setEnabled(false);
} else if (a.getSource () == button5) {
button5.setText(letter);
button5.setEnabled(false);
} else if (a.getSource () == button6) {
button6.setText(letter);
button6.setEnabled(false);
} else if (a.getSource () == button7) {
button7.setText(letter);
button7.setEnabled(false);
} else if (a.getSource () == button8) {
button8.setText(letter);
button8.setEnabled(false);
} else if (a.getSource () == button9) {
button9.setText(letter);
button9.setEnabled(false);
}
// * Determine who won */
// horizontal wins
if ( button1.getText () == button2.getText ()
&& button2.getText () == button3.getText ()
&& button1.getText () != "") {
win = true;
} else if ( button4.getText () == button5.getText ()
&& button5.getText () == button6.getText ()
&& button4.getText () != "") {
win = true;
} else if ( button7.getText () == button8.getText ()
&& button8.getText () == button9.getText ()
&& button7.getText () != "") {
win = true;
// Verticle wins
} else if (button1.getText() == button4.getText ()
&& button4.getText() == button7.getText ()
&& button1.getText() != "") {
win = true;
} else if (button2.getText() == button5.getText()
&& button5.getText() == button8.getText()
&& button2.getText() != "") {
win = true;
} else if ( button3.getText() == button6.getText()
&& button6.getText() == button9.getText()
&& button9.getText() != "") {
win = true ;
// Diagonal wins
} else if (button1.getText() == button5.getText()
&& button5.getText() == button9.getText()
&& button1.getText() != "") {
win = true;
} else if (button3.getText() == button5.getText()
&& button5.getText() == button7.getText()
&& button3.getText() != "") {
win = true;
} else {
win = false ;
}
/* show a dialog is someone wins or the game is tie*/
if ( win == true) {
JOptionPane.showMessageDialog(null, letter + " YOU WIN!");
} else if (count == 9 && win == false) {
JOptionPane.showMessageDialog ( null , " Tie Game!" ) ;
}
}
public static void main (String [] args) {
new michaeltictactoe2 () ;
}
}
error:
---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.6.0_22\bin\java.exe" michaeltictactoe1
java.lang.NoClassDefFoundError: michaeltictactoe1 (wrong name: test/rim/bbapps/testcase/lib/michaeltictactoe1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: michaeltictactoe1. Program will exit.
Exception in thread "main"
> Terminated with exit code 1.
thanks again.
You are compiling michaeltictactoe1 and the class name is michaeltictactoe2
compile with
javac michaeltictactoe2.java
then run with
java michaeltictactoe2
read http://www.oracle.com/technetwork/java/compile-136656.html
Looks like the class name does not match the file name (michaeltictactoe1 vs. michaeltictactoe2)

Categories