Sign in to follow this  
JhomarA.StaCruz

AI Producing Random Moves for this Game

Recommended Posts

I really need help on making AI that produce random moves on this game..

[url="http://en.wikipedia.org/wiki/Game_of_the_Generals"]http://en.wikipedia....of_the_Generals[/url]

I already finished the game but without the AI..

[code]
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.text.Document;



public class SalpakanMain
implements ActionListener, WindowFocusListener
{



private JPanel mainPanel;
private JPanel ChatBoxPanel;
private JPanel PiecesPanel;
private JPanel ConfirmPanel;
private JPanel NewGPanel;
private JPanel AboutPanel;
private JPanel editorPanel;


private ImageIcon Logo;
private static final String newline = "\n";
private String strEnemyX;
private String strEnemyY;
private String url;
private String selfIP;

private JScrollPane scroller;
private JScrollPane TextScrollPane;
private JFrame frame;
private JFrame PiecesFrame;
private JFrame InstructionFrame;
private JFrame ConnectWindow;
private JFrame ConfirmMove;
private JFrame ConfirmNewG;
private JButton ConfirmButton;
private JButton YesButton;
private JButton NoButton;
private JButton NewGameYes;
private JButton NewGameNo;
private JButton Board[][];
private JButton btnPieces[];
private JButton ClientButton;
private int intPieces[];
private JLabel AskMovement;
private JLabel AskNewG;
private JLabel LogoLabel;
private JMenuBar menuBar;
private JMenu menu, submenu;
private JMenuItem Host;
private JMenuItem AImode;
private JMenuItem Peer;
private JMenuItem Help;
private JMenuItem exit;
private JMenuItem NewG;
private JMenuItem Units;
private JMenuItem Pieces;
private JMenuItem Instructions;
private JMenuItem About;
private boolean PiecesLaidDown;
private boolean Selected;
private boolean MovePiece;
private boolean Confirm;
private boolean YourTurn;
private boolean Moving;
private boolean WaitingConfirm;
private boolean Proceed;
private int intBoardCountOne;
private int intBoardCountTwo;
private int PieceSelected;
private int intBoardTempOne;
private int intBoardTempTwo;
private int PieceCount;
private int intCurrentX;
private int intCurrentY;
private int intEnemyX;
private int intEnemyY;
private int BRank;
private int ARank;
private Socketmaster ChatSocket;
private Socketmaster MPSocket;
private Language Sender;
private JTextField IPfield;
private JTextField MPTxtField;
private JTextField Display;
private JTextField IPBox;
private JTextField InvisbleField;
private JTextArea txtArea;
private JTextArea InstructionArea;

public SalpakanMain()
{
Logo = new ImageIcon("About.gif");
strEnemyX = "";
strEnemyY = "";
selfIP = "";
AboutPanel = new JPanel();
editorPanel = new JPanel();
scroller = new JScrollPane();
frame = new JFrame("Salpakan (Games of the Generals)");
PiecesFrame = new JFrame("Piece Selection");
InstructionFrame = new JFrame("InstructionFrame");
ConnectWindow = new JFrame("Connection");
ConfirmMove = new JFrame("Confirm Movement");
ConfirmNewG = new JFrame("New Game?");
Board = new JButton[8][9];
btnPieces = new JButton[15];
ClientButton = new JButton("Connect");
intPieces = new int[15];
LogoLabel = new JLabel(Logo);
menuBar = new JMenuBar();
PiecesLaidDown = false;
Selected = false;
MovePiece = false;
Confirm = false;
YourTurn = false;
Moving = false;
WaitingConfirm = false;
Proceed = false;
PieceSelected = 0;
intBoardTempOne = 0;
intBoardTempTwo = 0;
PieceCount = 0;
intCurrentX = 0;
intCurrentY = 0;
ChatSocket = null;
MPSocket = null;
Sender = null;
IPfield = new JTextField("IP Address of the Server");
mainPanel = new JPanel();
ChatBoxPanel = new JPanel();
PiecesPanel = new JPanel();
ChatBoxPanel.setLayout(new BorderLayout());
mainPanel.setLayout(null);
PiecesPanel.setLayout(null);
ConfirmPanel = new JPanel();
ConfirmPanel.setLayout(null);
NewGPanel = new JPanel();
NewGPanel.setLayout(null);
AboutPanel.setLayout(new BorderLayout());
editorPanel.setLayout(new BorderLayout());
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(3);
frame.setResizable(false);
PiecesFrame.setDefaultCloseOperation(1);
PiecesFrame.setLocation(650, 60);
PiecesFrame.setResizable(false);
InstructionFrame.setLocation(190, 50);
InstructionFrame.setResizable(false);
InstructionFrame.addWindowFocusListener(this);
InstructionFrame.setDefaultCloseOperation(1);
ConfirmMove.setDefaultCloseOperation(0);
ConfirmMove.setLocation(190, 200);
ConfirmMove.setResizable(false);
ConfirmMove.addWindowFocusListener(this);
YesButton = new JButton("Yes");
NoButton = new JButton("No");
AskMovement = new JLabel("Are you sure you want to move there?");
YesButton.addActionListener(this);
NoButton.addActionListener(this);
YesButton.setBounds(30, 70, 100, 25);
NoButton.setBounds(160, 70, 100, 25);
AskMovement.setBounds(40, 20, 250, 25);
ConfirmPanel.add(AskMovement);
ConfirmPanel.add(NoButton);
ConfirmPanel.add(YesButton);
ConfirmNewG.setDefaultCloseOperation(0);
ConfirmNewG.setLocation(190, 200);
ConfirmNewG.setResizable(false);
ConfirmNewG.addWindowFocusListener(this);
NewGameYes = new JButton("Yes");
NewGameNo = new JButton("No");
AskNewG = new JLabel("Are you sure you want to start a new game?");
NewGameYes.addActionListener(this);
NewGameNo.addActionListener(this);
NewGameYes.setBounds(30, 70, 100, 25);
NewGameNo.setBounds(160, 70, 100, 25);
AskNewG.setBounds(40, 20, 250, 25);
NewGPanel.add(AskNewG);
NewGPanel.add(NewGameNo);
NewGPanel.add(NewGameYes);
ConnectWindow.addWindowFocusListener(this);
txtArea = new JTextArea(6, 40);
txtArea.setMargin(new Insets(0, 0, 0, 0));
txtArea.setEditable(false);
txtArea.setText(" Welcome to Salpakan (Games of the General) \n Developed by: Jhomar A. Sta. Cruz & Dan Christopher E. Sy \n Enjoy Playing! \n");
TextScrollPane = new JScrollPane(txtArea);
ChatBoxPanel.add(TextScrollPane, "North");
InstructionArea = new JTextArea(6, 40);
InstructionArea.setMargin(new Insets(0, 0, 0, 0));
InstructionArea.setEditable(false);
AboutPanel.add(editorPanel, "Center");
editorPanel.add(scroller, "Center");
scroller.getViewport().add(InstructionArea);
Display = new JTextField();
Display.setEditable(true);
Display.addActionListener(this);
ChatBoxPanel.add(Display);
InvisbleField = new JTextField();
InvisbleField.addActionListener(this);
MPTxtField = new JTextField("");
MPTxtField.addActionListener(this);
Board = new JButton[8][9];

//Draw the board of the GotG
for(int i = 0; i <= 7; i++)
{
for(int i1 = 0; i1 <= 8; i1++)
{
Board[i][i1] = new JButton("");
Board[i][i1].setBackground(Color.lightGray);
Board[i][i1].addActionListener(this);
mainPanel.add(Board[i][i1]);
}

}

for(int j = 0; j <= 7; j++)
{
for(int j1 = 0; j1 <= 8; j1++)
{
Board[j][j1].setBounds(j1 * 56 + 72, j * 52 + 10, 56, 52);
}

}

//Put Images on PiecesPanel on the Left Side
ConfirmButton = new JButton("Start!");
ConfirmButton.addActionListener(this);
ConfirmButton.setEnabled(false);
PiecesPanel.add(ConfirmButton);
//Size of the Panel
ConfirmButton.setBounds(75, 210, 100, 25);
//For loop starts here to draw the Pieces
// k = 14 because GotG has 14 Differnt Pieces
for(int k = 0; k <= 14; k++)
{
// K == 12 because players have 6 Privates.
if(k == 12)
{
intPieces[k] = 6;
} else
// K == 13 because players have 2 Spy.
if(k == 13)
{
intPieces[k] = 2;
} else
// All other ranks will contain only 1 piece.
{
intPieces[k] = 1;
}
// Importing the Image of the Pieces.
// Check if this is not a Private or Spy.
//If condition is met it will import images for other ranks that only contain 1 piece.
if(k != 13 && k != 14)
{
url = "Images/"+ (14 - (k + 1)) + ".gif";
btnPieces[k] = new JButton("" + (14 - (k + 1)));
} else
// If this is spy import this image
if(k == 13)
{
url = "Images/14.gif";
btnPieces[k] = new JButton("14");
} else
// If this is flag import this image.
if(k == 14)
{
url = "Images/0.gif";
btnPieces[k] = new JButton("0");
}
setPiecePic(k, url);
btnPieces[k].addActionListener(this);
btnPieces[k].setBorderPainted(false);
//Add the btnPieces to the Pieces Panel
PiecesPanel.add(btnPieces[k]);
}

int k1 = 0;
int l1 = 0;
for(int l = 0; l <= 14; l++)
{
btnPieces[l].setBounds(l1 * 56 + 10, k1 * 52, 56, 52);
if(++l1 == 4)
{
l1 = 0;
k1++;
}
}

menu = new JMenu("Mode");
menu.setMnemonic(89);
menuBar.add(menu);
submenu = new JMenu("Two Players");
submenu.setMnemonic(91);
menu.add(submenu);
Host = new JMenuItem("Start Server", 83);
Host.setAccelerator(KeyStroke.getKeyStroke(83, 2));
Host.addActionListener(this);
Host.setActionCommand("Host");
submenu.add(Host);
AImode = new JMenuItem("One Player", 90);
AImode.setAccelerator(KeyStroke.getKeyStroke(90, 2));
AImode.addActionListener(this);
AImode.setActionCommand("One Player");
menu.add(AImode);
Peer = new JMenuItem("Connect to Server", 67);
Peer.setAccelerator(KeyStroke.getKeyStroke(67, 2));
Peer.addActionListener(this);
Peer.setActionCommand("Peer");
submenu.add(Peer);
menu = new JMenu("Game");
menu.setMnemonic(71);
menuBar.add(menu);
NewG = new JMenuItem("New Game", 78);
NewG.setAccelerator(KeyStroke.getKeyStroke(78, 2));
NewG.addActionListener(this);
NewG.setActionCommand("New Game");
NewG.setEnabled(false);
menu.add(NewG);
menu = new JMenu("View");
menu.setMnemonic(86);
menuBar.add(menu);
Pieces = new JMenuItem("Piece Selection", 80);
Pieces.setAccelerator(KeyStroke.getKeyStroke(80, 2));
Pieces.addActionListener(this);
Pieces.setActionCommand("Piece");
menu.add(Pieces);
menu = new JMenu("Help");
menu.setMnemonic(72);
menuBar.add(menu);
Instructions = new JMenuItem("Instructions", 73);
Instructions.setAccelerator(KeyStroke.getKeyStroke(73, 2));
Instructions.addActionListener(this);
Instructions.setActionCommand("Instructions");
menu.add(Instructions);
menu.addSeparator();
About = new JMenuItem("About Us", 65);
About.setAccelerator(KeyStroke.getKeyStroke(65, 2));
About.addActionListener(this);
About.setActionCommand("About");
menu.add(About);
frame.setJMenuBar(menuBar);
frame.getContentPane().add("South", ChatBoxPanel);
PiecesPanel.setBounds(620, 0, 250 ,280);
frame.add(PiecesPanel);
frame.getContentPane().add(mainPanel);
//PiecesFrame.setContentPane(PiecesPanel);
ConfirmMove.setContentPane(ConfirmPanel);
ConfirmNewG.setContentPane(NewGPanel);
InstructionFrame.setContentPane(AboutPanel);
InstructionFrame.setContentPane(editorPanel);
frame.pack();
ConfirmMove.pack();
ConfirmNewG.pack();
PiecesFrame.pack();
InstructionFrame.pack();
frame.setSize(900, 600);
PiecesFrame.setSize(250, 280);
ConfirmMove.setSize(300, 150);
ConfirmNewG.setSize(300, 150);
InstructionFrame.setSize(600, 400);
frame.setVisible(true);
//PiecesFrame.setVisible(true);
ConfirmMove.setVisible(false);
ConfirmNewG.setVisible(false);
InstructionFrame.setVisible(false);
frame.requestFocus();
DisableBoard();
DisablePiece();
}

public void actionPerformed(ActionEvent actionevent)
{
if(actionevent.getActionCommand().equals("Host"))
{
YourTurn = true;
try {
selfIP = ipconfig.ip();
} catch (SocketException ex) {
Logger.getLogger(SalpakanMain.class.getName()).log(Level.SEVERE, null, ex);
}
txtArea.append("\n<System Hosting> Your IP is: " + selfIP.substring(1));
ChatSocket = new Socketmaster(InvisbleField, 4444);
Thread thread = new Thread(ChatSocket);
thread.start();
MPSocket = new Socketmaster(MPTxtField, 4445);
Thread thread2 = new Thread(MPSocket);
thread2.start();
Sender = new Language(MPSocket);
Host.removeActionListener(this);
Host.setEnabled(false);
Peer.removeActionListener(this);
Peer.setEnabled(false);
NewG.setEnabled(true);
}
if(actionevent.getActionCommand().equals("Peer") && LoneWindow())
{
JPanel jpanel = new JPanel();
jpanel.setLayout(null);
JLabel jlabel = new JLabel("Host IP or DNS:");
ClientButton.setBounds(240, 17, 100, 30);
IPfield.setBounds(5, 20, 220, 25);
jlabel.setBounds(7, 0, 100, 25);
IPfield.addActionListener(this);
ClientButton.addActionListener(this);
jpanel.add(jlabel);
jpanel.add(ClientButton);
jpanel.add(IPfield);
ConnectWindow.setDefaultCloseOperation(1);
ConnectWindow.setContentPane(jpanel);
ConnectWindow.setResizable(false);
ConnectWindow.pack();
ConnectWindow.setSize(new Dimension(350, 90));
ConnectWindow.setVisible(true);
NewG.setEnabled(true);
}
if(actionevent.getSource() == ClientButton)
{
YourTurn = false;
ChatSocket = new Socketmaster(InvisbleField, IPfield.getText(), 4444);
Thread thread1 = new Thread(ChatSocket);
thread1.start();
MPSocket = new Socketmaster(MPTxtField, IPfield.getText(), 4445);
Thread thread3 = new Thread(MPSocket);
thread3.start();
Sender = new Language(MPSocket);
try
{
Thread.sleep(2000L);
}
catch(InterruptedException interruptedexception2) { }
Host.removeActionListener(this);
Peer.removeActionListener(this);
ConnectWindow.setVisible(false);
EnablePiece();
ReturnBoard();
Sender.enable();
}
if(actionevent.getActionCommand().equals("Instructions") && LoneWindow())
{
InstructionFrame.setTitle("Instructions");
editorPanel.add(scroller);
editorPanel.remove(LogoLabel);
InstructionFrame.setVisible(true);
scroller.setHorizontalScrollBarPolicy(30);
scroller.setVerticalScrollBarPolicy(20);
try
{
InstructionArea.read(new FileReader("Instruction.Gotg"), null);
}
catch(IOException ioexception) { }
}
if(actionevent.getActionCommand().equals("One Player") && LoneWindow())
{
newgamemethod();
}
if(actionevent.getActionCommand().equals("About") && LoneWindow())
{
InstructionFrame.setTitle("About Us");
InstructionFrame.setVisible(true);
editorPanel.remove(scroller);
editorPanel.add(LogoLabel);
InstructionFrame.pack();
}
if(actionevent.getActionCommand().equals("Piece"))
{
if(PiecesFrame.isVisible())
{
PiecesFrame.setVisible(false);
} else
{
PiecesFrame.setVisible(true);
}
frame.requestFocus();
}
if(actionevent.getSource() == Display)
{
if(ChatSocket != null)
{
try
{
Thread.sleep(40L);
}
catch(InterruptedException interruptedexception) { }
ChatSocket.sendText(Display.getText());
}
txtArea.append("\n<Self> " + Display.getText());
txtArea.setCaretPosition(txtArea.getDocument().getLength());
Display.setText("");
}
if(actionevent.getSource() == InvisbleField)
{
if(InvisbleField.getText().charAt(0) != 'z')
{
txtArea.append("\n<Peer> " + InvisbleField.getText());
} else
{
txtArea.append("\n" + InvisbleField.getText().substring(1));
}
txtArea.setCaretPosition(txtArea.getDocument().getLength());
}
if(!Confirm && !WaitingConfirm)
{
for(int i = 0; i <= 14; i++)
{
if(actionevent.getSource() != btnPieces[i] || Selected)
{
continue;
}
Selected = true;
for(intBoardCountOne = 0; intBoardCountOne <= 4; intBoardCountOne++)
{
for(intBoardCountTwo = 0; intBoardCountTwo <= 8; intBoardCountTwo++)
{
Board[intBoardCountOne][intBoardCountTwo].setEnabled(false);
}

}

intPieces[i]--;
if(intPieces[i] == 0)
{
btnPieces[i].setEnabled(false);
}
PieceSelected = i;
PieceCount++;
}

for(intBoardCountOne = 4; intBoardCountOne <= 7; intBoardCountOne++)
{
for(intBoardCountTwo = 0; intBoardCountTwo <= 8; intBoardCountTwo++)
{
if(actionevent.getSource() == Board[intBoardCountOne][intBoardCountTwo] && Selected && !MovePiece)
{
if(Board[intBoardCountOne][intBoardCountTwo].getText() != "")
{
continue;
}
Board[intBoardCountOne][intBoardCountTwo].setText(btnPieces[PieceSelected].getText());
setPic(intBoardCountOne, intBoardCountTwo, btnPieces[PieceSelected].getText());
Selected = false;
Sender.sendPos(intBoardCountTwo, intBoardCountOne);
if(PieceCount == 21)
{
ConfirmButton.setEnabled(true);
}
continue;
}
if(actionevent.getSource() == Board[intBoardCountOne][intBoardCountTwo] && !Selected)
{
if(Board[intBoardCountOne][intBoardCountTwo].getText() != "")
{
Selected = true;
MovePiece = true;
intBoardTempOne = intBoardCountOne;
intBoardTempTwo = intBoardCountTwo;
}
continue;
}
if(actionevent.getSource() == Board[intBoardCountOne][intBoardCountTwo] && MovePiece && Board[intBoardCountOne][intBoardCountTwo].getText() == "")
{
Board[intBoardCountOne][intBoardCountTwo].setText(Board[intBoardTempOne][intBoardTempTwo].getText());
setPic(intBoardCountOne, intBoardCountTwo, Board[intBoardTempOne][intBoardTempTwo].getText());
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.sendPos(intBoardCountTwo, intBoardCountOne);
MovePiece = false;
Selected = false;
}
}

}

} else
if(Confirm && WaitingConfirm && YourTurn)
{
for(intBoardCountOne = 0; intBoardCountOne <= 7; intBoardCountOne++)
{
for(intBoardCountTwo = 0; intBoardCountTwo <= 8; intBoardCountTwo++)
{
if(actionevent.getSource() == Board[intBoardCountOne][intBoardCountTwo] && !Moving && Board[intBoardCountOne][intBoardCountTwo].getText() != "" && Board[intBoardCountOne][intBoardCountTwo].getText() != "enemy")
{
intBoardTempOne = intBoardCountOne;
intBoardTempTwo = intBoardCountTwo;
intCurrentX = intBoardCountTwo;
intCurrentY = intBoardCountOne;
ShowMoves(intBoardCountOne, intBoardCountTwo);
Moving = true;
continue;
}
if(actionevent.getSource() != Board[intBoardCountOne][intBoardCountTwo] || !Moving)
{
continue;
}
if(intBoardCountOne == intBoardTempOne && intBoardCountTwo == intBoardTempTwo)
{
Moving = false;
ReturnBoard();
continue;
}
if((Board[intBoardCountOne][intBoardCountTwo].getText() == "" || Board[intBoardCountOne][intBoardCountTwo].getText().equalsIgnoreCase("enemy")) && LoneWindow())
{
intCurrentX = intBoardCountTwo;
intCurrentY = intBoardCountOne;
ConfirmMove.setVisible(true);
}
}

}

}
if(actionevent.getSource() == YesButton && Board[intCurrentY][intCurrentX].getText().equalsIgnoreCase("enemy"))
{
ARank = Integer.parseInt(Board[intBoardTempOne][intBoardTempTwo].getText());
System.out.println(ARank);
Sender.GrabInfo(intCurrentX, intCurrentY);
try
{
Thread.sleep(50L);
}
catch(InterruptedException interruptedexception1) { }
YourTurn = false;
Sender.setTurn("true");
if(ARank == BRank)
{
txtArea.append("\n<System> Both Pieces have died coming out of this battle");
ChatSocket.sendText("z<System> Both Pieces have died coming out of this battle");
Board[intBoardTempOne][intBoardTempTwo].setText("");
Board[intCurrentY][intCurrentX].setText("");
removePic(intCurrentY, intCurrentX);
removePic(intBoardTempOne, intBoardTempTwo);
Sender.EraseEnemy(intCurrentX, intCurrentY);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
} else
if(ARank == 1 && BRank == 14)
{
txtArea.append("\n<System> You have come out victorious, from your battle.");
ChatSocket.sendText("z<System> You have been defeated through a horrendous battle.");
Board[intCurrentY][intCurrentX].setText(Board[intBoardTempOne][intBoardTempTwo].getText());
setPic(intCurrentY, intCurrentX, Board[intBoardTempOne][intBoardTempTwo].getText());
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.sendPos(intCurrentX, intCurrentY);
} else
if(ARank == 14 && BRank == 1)
{
txtArea.append("\n<System> You have been defeated through a horrendous battle");
ChatSocket.sendText("z<System> You have come out victorious, from your battle.");
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Board[intCurrentY][intCurrentX].setText("enemy");
setPic(intCurrentY, intCurrentX, "enemy");
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.EraseEnemy(intCurrentX, intCurrentY);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
} else
if(ARank > BRank)
{
txtArea.append("\n<System> You have come out victorious from your battle.");
ChatSocket.sendText("z<System> You have been defeated through a horrendous battle.");
Board[intCurrentY][intCurrentX].setText(Board[intBoardTempOne][intBoardTempTwo].getText());
setPic(intCurrentY, intCurrentX, Board[intBoardTempOne][intBoardTempTwo].getText());
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.sendPos(intCurrentX, intCurrentY);
} else
if(ARank < BRank)
{
txtArea.append("\n<System> You have been defeated through a horrendous battle");
ChatSocket.sendText("z<System> You have come out victorious, from your battle.");
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Board[intCurrentY][intCurrentX].setText("enemy");
setPic(intCurrentY, intCurrentX, "enemy");
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.EraseEnemy(intCurrentX, intCurrentY);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
}
if(ARank == 0 && BRank == 0)
{
setPic(intCurrentY, intCurrentX, "" + BRank);
setPic(intBoardTempTwo, intBoardTempTwo, "" + ARank);
Sender.SendLabel(ARank, intBoardTempTwo, intBoardTempOne);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
Sender.sendYouWin();
txtArea.append("\n<System> Tie game!!");
DisableBoard();
Sender.sendTie();
} else
if(ARank == 0)
{
setPic(intCurrentY, intCurrentX, "" + BRank);
setPic(intBoardTempTwo, intBoardTempTwo, "" + ARank);
Sender.SendLabel(ARank, intBoardTempTwo, intBoardTempTwo);
Sender.sendYouWin();
txtArea.append("\n<System> Good game, try again next time.");
txtArea.append("\n<System> Winner will choose to start next game or not.");
DisableBoard();
} else
if(BRank == 0)
{
setPic(intCurrentY, intCurrentX, "" + BRank);
setPic(intBoardTempTwo, intBoardTempTwo, "" + ARank);
Sender.SendLabel(ARank, intBoardTempTwo, intBoardTempTwo);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
txtArea.append("\n<System> Congratulations, you WIN!");
DisableBoard();
Sender.sendYouLose();
ConfirmNewG.setVisible(true);
}
ConfirmMove.setVisible(false);
Moving = false;
ReturnBoard();
} else
if(actionevent.getSource() == YesButton && !Board[intCurrentY][intCurrentX].getText().equalsIgnoreCase("enemy"))
{
ARank = Integer.parseInt(Board[intBoardTempOne][intBoardTempTwo].getText());
if(ARank == 0 && intCurrentY == 0)
{
txtArea.append("\n<System> Congratulations, you WIN!");
DisableBoard();
Sender.sendYouLose();
ConfirmNewG.setVisible(true);
}
YourTurn = false;
Sender.setTurn("true");
Board[intCurrentY][intCurrentX].setText(Board[intBoardTempOne][intBoardTempTwo].getText());
setPic(intCurrentY, intCurrentX, Board[intBoardTempOne][intBoardTempTwo].getText());
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
ConfirmMove.setVisible(false);
Sender.sendPos(intCurrentX, intCurrentY);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Moving = false;
ReturnBoard();
} else
if(actionevent.getSource() == NoButton)
{
Moving = false;
ReturnBoard();
ConfirmMove.setVisible(false);
}
if(actionevent.getSource() == ConfirmButton && !WaitingConfirm)
{
WaitingConfirm = true;
Sender.sendBoolean();
ReturnBoard();
for(int j = 0; j <= 14; j++)
{
if(j == 12)
{
intPieces[j] = 6;
} else
if(j == 13)
{
intPieces[j] = 2;
} else
{
intPieces[j] = 1;
}
btnPieces[j].setText("" + intPieces[j]);
btnPieces[j].setEnabled(true);
btnPieces[j].removeActionListener(this);
}

ConfirmButton.setEnabled(false);
if(!MPTxtField.getText().equalsIgnoreCase("true"))
{
txtArea.append("\n<System> Waiting for other user to confirm....");
}
}
if(MPTxtField.getText().equalsIgnoreCase("true") && WaitingConfirm)
{
Confirm = true;
txtArea.append("\n<System> Opponent has confirmed, game starting.");
MPTxtField.setText(" ");
}
if(actionevent.getSource() == MPTxtField && MPTxtField.getText() != "" && MPTxtField != null)
{
if(MPTxtField.getText().equalsIgnoreCase("START"))
{
EnablePiece();
ReturnBoard();
}
if(MPTxtField.getText().equalsIgnoreCase("YOUWIN"))
{
txtArea.append("\n<System> Congratulations, you WIN!");
DisableBoard();
ConfirmNewG.setVisible(true);
} else
if(MPTxtField.getText().equalsIgnoreCase("YOULOSE"))
{
txtArea.append("\n<System> Good game, try again next time.");
txtArea.append("\n<System> Winner will choose to start next game or not.");
DisableBoard();
} else
if(MPTxtField.getText().equalsIgnoreCase("TIE"))
{
txtArea.append("\n<System> Tie game!!");
txtArea.append("\n<System> Host will choose to start next game or not.");
DisableBoard();
}
if(MPTxtField.getText().equalsIgnoreCase("NEWGAME"))
{
txtArea.append("\n<System> Game Ended.");
txtArea.append("\n<System> Opponent requested New Game.");
txtArea.append("\n<System> Waiting for Confirm...");
newgamemethod();
}
if(MPTxtField.getText().charAt(0) == 'p')
{
MakeIntegers(1, 2);
Board[7 - intEnemyY][8 - intEnemyX].setText("enemy");
setPic(7 - intEnemyY, 8 - intEnemyX, "enemy");
}
if(MPTxtField.getText().charAt(0) == 'x')
{
if(MPTxtField.getText().substring(1).equals("true"))
{
YourTurn = true;
txtArea.append("\n<System> It Is Your Turn To Move");
txtArea.setCaretPosition(txtArea.getDocument().getLength());
} else
{
YourTurn = false;
}
} else
if(MPTxtField.getText().charAt(0) == 'e')
{
MakeIntegers(1, 2);
Board[7 - intEnemyY][8 - intEnemyX].setText("");
removePic(7 - intEnemyY, 8 - intEnemyX);
} else
if(MPTxtField.getText().charAt(0) == 'y')
{
BRank = Integer.parseInt(MPTxtField.getText().substring(1));
} else
if(MPTxtField.getText().charAt(0) == 'b')
{
MakeIntegers(1, 2);
Sender.sendPiece(Board[7 - intEnemyY][8 - intEnemyX].getText());
} else
if(MPTxtField.getText().charAt(0) == 'l')
{
if(MPTxtField.getText().length() > 4)
{
MakeIntegers(3, 4);
Board[7 - intEnemyY][8 - intEnemyX].setText(MPTxtField.getText().substring(1, 3));
setPic(7 - intEnemyY, 8 - intEnemyX, MPTxtField.getText().substring(1, 3));
} else
{
MakeIntegers(2, 3);
Board[7 - intEnemyY][8 - intEnemyX].setText(MPTxtField.getText().substring(1, 2));
setPic(7 - intEnemyY, 8 - intEnemyX, MPTxtField.getText().substring(1, 2));
}
}
}
if(actionevent.getSource() == NewGameYes)
{
txtArea.append("\n<System> New Game Started!");
Sender.NewGameRequest();
newgamemethod();
Sender.setTurn("false");
YourTurn = true;
ConfirmNewG.setVisible(false);
} else
if(actionevent.getSource() == NewGameNo)
{
ConfirmNewG.setVisible(false);
}
if(actionevent.getActionCommand().equals("New Game"))
{
ConfirmNewG.setVisible(true);
}
}

public void windowGainedFocus(WindowEvent windowevent)
{
}

public void windowLostFocus(WindowEvent windowevent)
{
if(windowevent.getSource() == ConfirmMove)
{
ConfirmMove.requestFocus();
}
if(windowevent.getSource() == ConnectWindow)
{
ConnectWindow.requestFocus();
}
if(windowevent.getSource() == ConfirmNewG)
{
ConfirmNewG.requestFocus();
}
if(windowevent.getSource() == InstructionFrame)
{
InstructionFrame.requestFocus();
}
}

private boolean LoneWindow()
{
return !ConfirmMove.isVisible() && !ConnectWindow.isVisible() && !ConfirmNewG.isVisible() && !InstructionFrame.isVisible();
}

public void newgamemethod()
{
PiecesLaidDown = false;
Selected = false;
MovePiece = false;
Confirm = false;
YourTurn = false;
Moving = false;
WaitingConfirm = false;
clearBoard();
PieceCount = 0;
for(int i = 0; i <= 14; i++)
{
if(i == 12)
{
intPieces[i] = 6;
} else
if(i == 13)
{
intPieces[i] = 2;
} else
{
intPieces[i] = 1;
}
if(i != 13 && i != 14)
{
btnPieces[i].setText("" + (14 - (i + 1)));
} else
if(i == 13)
{
btnPieces[i].setText("14");
} else
if(i == 14)
{
btnPieces[i].setText("0");
}
btnPieces[i].addActionListener(this);
btnPieces[i].setBorderPainted(false);
}

EnablePiece();
ReturnBoard();
}

private void MakeIntegers(int i, int j)
{
strEnemyX = MPTxtField.getText().substring(i, i + 1);
strEnemyY = MPTxtField.getText().substring(j);
intEnemyX = Integer.parseInt(strEnemyX);
intEnemyY = Integer.parseInt(strEnemyY);
}

private void ShowMoves(int i, int j)
{
DisableBoard();
for(int k = -1; k <= 1; k++)
{
if(i + k >= 0 && i + k <= 7 && (Board[i + k][j].getText() == "" || Board[i + k][j].getText() == "enemy"))
{
Board[i + k][j].setEnabled(true);
}
if(j + k >= 0 && j + k <= 8 && (Board[i][j + k].getText() == "" || Board[i][j + k].getText() == "enemy"))
{
Board[i][j + k].setEnabled(true);
}
if(k == 0)
{
Board[i][j].setEnabled(true);
}
}

}

private void EnablePiece()
{
for(int i = 0; i < 15; i++)
{
btnPieces[i].setEnabled(true);
}

}

private void DisablePiece()
{
for(int i = 0; i < 15; i++)
{
btnPieces[i].setEnabled(false);
}

}

private void ReturnBoard()
{
for(intBoardCountOne = 0; intBoardCountOne <= 7; intBoardCountOne++)
{
for(intBoardCountTwo = 0; intBoardCountTwo <= 8; intBoardCountTwo++)
{
Board[intBoardCountOne][intBoardCountTwo].setEnabled(true);
}

}

}

private void DisableBoard()
{
for(intBoardCountOne = 0; intBoardCountOne <= 7; intBoardCountOne++)
{
for(intBoardCountTwo = 0; intBoardCountTwo <= 8; intBoardCountTwo++)
{
Board[intBoardCountOne][intBoardCountTwo].setEnabled(false);
}

}

}

private void clearBoard()
{
for(intBoardCountOne = 0; intBoardCountOne <= 7; intBoardCountOne++)
{
for(intBoardCountTwo = 0; intBoardCountTwo <= 8; intBoardCountTwo++)
{
Board[intBoardCountOne][intBoardCountTwo].setText("");
removePic(intBoardCountOne, intBoardCountTwo);
}

}

}

public int getPieceID()
{
return Integer.parseInt(Board[intCurrentY][intCurrentX].getText());
}

public void setPic(int i, int j, String s)
{
if(s != "enemy")
{
url ="Images/" + s + ".gif";
} else
{
url ="Images/enemy.gif";
}
Board[i][j].setForeground(Color.white);
Board[i][j].setVerticalTextPosition(1);
Board[i][j].setIconTextGap(-8);
Board[i][j].setHorizontalTextPosition(4);
Board[i][j].setIconTextGap(-10);
Board[i][j].setIcon(new ImageIcon(url));
Board[i][j].setHorizontalAlignment(0);
}

public void setPiecePic(int i, String s)
{
btnPieces[i].setForeground(Color.white);
btnPieces[i].setVerticalTextPosition(1);
btnPieces[i].setIconTextGap(-8);
btnPieces[i].setHorizontalTextPosition(4);
btnPieces[i].setIconTextGap(-10);
btnPieces[i].setIcon(new ImageIcon(s));
btnPieces[i].setHorizontalAlignment(0);
}

public void removePic(int i, int j)
{
Board[i][j].setForeground(Color.white);
Board[i][j].setVerticalTextPosition(1);
Board[i][j].setIconTextGap(-8);
Board[i][j].setHorizontalTextPosition(4);
Board[i][j].setIconTextGap(-10);
Board[i][j].setIcon(null);
Board[i][j].setHorizontalAlignment(0);
}

public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {

public void run()
{
SalpakanMain gotg = new SalpakanMain();
}

});
}
}

[/code]

This part of the 1 player is missing, i dont know what to put to generate random moves and random position of the pieces.

[code]
if(actionevent.getActionCommand().equals("One Player") && LoneWindow())
{
newgamemethod();
}

[/code]

Please help me.. I really need this project to be done..

Thank you and Godbless.

Share this post


Link to post
Share on other sites
[quote name='jsaade' timestamp='1310365523' post='4833604']
random moves is not AI.
[/quote]

Of course it is. Not only that, it's usually a very good place to start. It allows you to build the interface between the AI and the rest of the game, so you can test everything. It also serves you as a baseline for improvements. You can even use it as playout policy in an MCTS implementation.

The code is quite unreadable. What problems are you having? My recollection of the game of the generals is that a move consists of picking a piece and a direction (North, East, South, West). This should be pretty straight forward.

Share this post


Link to post
Share on other sites
Do you have anything that i could start reading on? Or sample code that produce random moves? Or any algorithm that i can use?

Thanks!

Alvaro, do you have any Games of the General that contains any computer Opponent?

Share this post


Link to post
Share on other sites
[quote name='saber210' timestamp='1310410246' post='4833903']
Do you have anything that i could start reading on? Or sample code that produce random moves? Or any algorithm that i can use?

Thanks!

Alvaro, do you have any Games of the General that contains any computer Opponent?
[/quote]

No, I only heard about the game of the generals in another thread in this forum, but I've never played it. I still don't know what problem you are having generating random moves. It's about 5 lines of code, mostly dealing with however you are representing the board.

Share this post


Link to post
Share on other sites
You want to shuffle the pieces randomly. The algorithm you should use is [url="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle"]this one[/url]. In C++ this algorithm is implemented by std::random_shuffle.

In pseudo-code:
[code]// Assume the pieces are in an array indexed by [0,n)
for (i=0; i<n; ++i) {
int random_index = i + random_number_smaller_than(n-i);
swap(piece_array[i], piece_array[random_index]);
}[/code]

Share this post


Link to post
Share on other sites
[img]http://public.gamedev.net/public/style_emoticons/default/dry.gif[/img]
Standing rules around GDNet is that we don't help people with their homework. It's a policy thing.

That said, what you are [being instructed] to do is really not that hard if you have any concept of the simplest programming techniques. Methinks you should study harder and then (and [i]this[/i] is the kicker) think through your problem one step at a time. The good news about programming is that it is logical by its very nature. If you can break down what it is you want to accomplish into meaningful parts, your program pretty much writes itself.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this