package uk.ac.glos.games.ct5025.s1705553.Games;
import uk.ac.glos.games.ct5025.s1705553.Model.Game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Snakes extends Game implements ActionListener
{
private final JTabbedPane tabPane;
private JPanel gamePanel;
private Icon p1;
private Icon p2;
private final Icon[][] icon= new Icon[10][10];
private Icon diceIcon;
private final JButton[][] grid=new JButton[10][10];
private JButton start;
private final JButton bPlayer1;
private final JButton bPlayer2;
private final JLabel dice;
private final Random randomNo;
private int imageFlag;
private int prevIp1;
private int prevJp1;
private int path;
private int p1value;
private int p2value;
private int gameOver;
// --Commented out by Inspection (10/27/2018 1:43 AM):private int cnoFlag=0;
private String str;
private final int[][] board={
{100,99,98,97,96,95,94,93,92,91},
{81,82,83,84,85,86,87,88,89,90},
{80,79,78,77,76,75,74,73,72,71},
{61,62,63,64,65,66,67,68,69,70},
{60,59,58,57,56,55,54,53,52,51},
{41,42,43,44,45,46,47,48,49,50},
{40,39,38,37,36,35,34,33,32,31},
{21,22,23,24,25,26,27,28,29,30},
{20,19,18,17,16,15,14,13,12,11},
{ 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
};
public Snakes()
{
JFrame f = new JFrame("Snakes and Ladders");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// JPanel mainPanel = new JPanel();
tabPane =new JTabbedPane(JTabbedPane.LEFT);
f.setLayout(new BorderLayout());
randomNo= new Random();
bPlayer1= new JButton("player1");
bPlayer2= new JButton("player2");
bPlayer1.setEnabled(false);
bPlayer2.setEnabled(false);
bPlayer1.addActionListener(this);
bPlayer2.addActionListener(this);
dice = new JLabel();
Game();
f.add(gamePanel,BorderLayout.CENTER);
f.setResizable(false);
f.setSize(900,900);
//f.pack();
f.setVisible(true);
}
private void Game()
{
JLabel l1= new JLabel();
JLabel l2= new JLabel();
start= new JButton("Start");
start.addActionListener(this);
gamePanel= new JPanel();
JPanel gameCenter = new JPanel();
JPanel gameWest = new JPanel();
new JPanel();
JPanel gameNorth;
new JPanel();
JPanel gameSouth;
gameCenter.setLayout(new GridLayout(10,10));
// JPanel gameEast = new JPanel(new GridLayout(2, 1));
gameWest.setLayout(new FlowLayout());
gameNorth =new JPanel(new FlowLayout());
gameSouth = new JPanel(new FlowLayout());
gamePanel.setLayout(new BorderLayout());
p1= new ImageIcon("images/snakesAndLadders/player1.gif");
p2= new ImageIcon("images/snakesAndLadders/player2.gif");
l1.setIcon(p1);
l2.setIcon(p2);
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
grid[i][j]= new JButton();
//b[i][j].addActionListener(this);
path=board[i][j];
str=Integer.toString(path);
icon[i][j]=new ImageIcon("images/snakesAndLadders/"+str+".jpg");
grid[i][j].setIcon(icon[i][j]);
}
str=Integer.toString(1);
diceIcon= new ImageIcon("images/snakesAndLadders/GameDice/"+str+".jpg");
dice.setIcon(diceIcon);
gameSouth.add(l1);
gameSouth.add(bPlayer1);
gameSouth.add(dice);
gameSouth.add(bPlayer2);
gameSouth.add(l2);
gameNorth.add(start);
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
gameCenter.add(grid[i][j]);
//System.out.print("["+i+"]["+j+"],");
}
//System.out.println();
}
gamePanel.add(gameCenter,BorderLayout.CENTER);
gamePanel.add(gameWest,BorderLayout.WEST);
gamePanel.add(gameNorth,BorderLayout.NORTH);
gamePanel.add(gameSouth,BorderLayout.SOUTH);
tabPane.addTab("Game",gamePanel);
}
@Override
protected void checkWin()
{
int number=randomNo.nextInt(3);
//System.out.println("Chance is :"+n) ;
if(number==0)
checkWin();
if(number==1)
{
bPlayer2.setEnabled(false);
bPlayer1.setEnabled(true);
}
else if(number==2)
{
bPlayer2.setEnabled(true);
bPlayer1.setEnabled(false);
}
}
public void actionPerformed(ActionEvent e)
{
int number;
try{
if(e.getSource()==bPlayer1)
{
//System.out.println("JB Player");
do{
number = rollDice();
}while(number==0);
//System.out.println("UR playerPass value is :"+n);
p1value=p1value+number;
if(p1value>=100)
{
p1value=100;
}
player1ImageTravel(p1value);
bPlayer2.setEnabled(true);
bPlayer1.setEnabled(false);
setBothImage();
}
else if(e.getSource()==bPlayer2)
{
//System.out.println("JB Computer");
do{
number = rollDice();
}while(number==0);
//System.out.println("Computer Pass Value is "+n);
p2value=p2value+number;
if(p2value>=100)
{
p2value=100;
}
player2ImageTravel(p2value);
bPlayer2.setEnabled(false);
bPlayer1.setEnabled(true);
setBothImage();
}
else if(e.getSource()==start)
{
gameOver=0;
p1value=0;
p2value=0;
//int imageFlag2 = 0;
rePrint();
start.setEnabled(false);
checkWin();
}
else
{
imageChange(e);
}
}catch(Exception ee)
{
//System.out.println("Error in BUTTON:"+ee);
}
}
private int rollDice()
{
int i = randomNo.nextInt(7);
str=Integer.toString(i);
diceIcon= new ImageIcon("images/snakesAndLadders/GameDice/"+str+".jpg");
dice.setIcon(diceIcon);
return i;
}
@Override
protected void win(int square)
{
for(int k=0;k<10;k++)
for(int l=0;l<10;l++)
{
gameOver=1;
if (JOptionPane.showConfirmDialog(new JFrame("Winner"),"You are the Winner!!, Would you like to play again?") == JOptionPane.YES_OPTION) {
restart();
} else {
System.exit(0);
}
}
}
@Override
protected void restart(){
p1value=0;
p2value=0;
rePrint();
}
private void rePrint()
{
try{
int i;
for(i =0; i <10; i++)
{
int j;
for(j =0; j <10; j++)
{
// grid[i][j].setIcon(null);
grid[i][j].setIcon(icon[i][j]);
}
}
}catch(Exception e)
{
//System.out.println("error in repaint"+e);
}
}
private void player1ImageTravel(int playerImage)
{
int i,j = 0;
int noFlag=0;
if(gameOver==0)
{
try{
rePrint();
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(playerImage==board[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
Thread.sleep(200);
grid[i][j].setIcon(p1);
prevIp1=i;
prevJp1=j;
if(playerImage==4)
{
playerImage=25;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==13)
{
playerImage=46;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==33)
{
playerImage=49;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==42)
{
playerImage=63;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==50)
{
playerImage=69;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==62)
{
playerImage=81;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==74)
{
playerImage=92;
player1ImageTravel(playerImage);
p1value=playerImage;
}
if(playerImage==99)
{
playerImage=41;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==89)
{
playerImage=53;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==76)
{
playerImage=58;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==66)
{
playerImage=45;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==54)
{
playerImage=31;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==43)
{
playerImage=18;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==40)
{
playerImage=3;
player1ImageTravel(playerImage);
p1value=playerImage;
}
else if(playerImage==27)
{
playerImage=5;
player1ImageTravel(playerImage);
p1value=playerImage;
}
}
if(playerImage>=100)
{
win(0);
}
}
catch(Exception ignored)
{
}
}
}
private void setBothImage()
{
int i;
int j=0;
int noFlag=0;
if(gameOver==0)
{
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(p1value==board[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
grid[i][j].setIcon(p1);
}
noFlag=0;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(p2value==board[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
grid[i][j].setIcon(p2);
}
}
}
private void player2ImageTravel(int playerImage)
{
int i,j = 0;
int noFlag=0;
try{
rePrint();
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(playerImage==board[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
path=board[prevIp1][prevJp1];
str=Integer.toString(path);
Thread.sleep(200);
icon[prevIp1][prevJp1]=new ImageIcon("images/snakesAndLadders/"+str+".jpg");
Thread.sleep(200);
grid[prevIp1][prevJp1].setIcon(icon[prevIp1][prevJp1]);
grid[i][j].setIcon(p1);
prevIp1=i;
prevJp1=j;
if(playerImage==4)
{
playerImage=25;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==13)
{
playerImage=46;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==33)
{
playerImage=49;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==42)
{
playerImage=63;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==50)
{
playerImage=69;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==62)
{
playerImage=81;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==74)
{
playerImage=92;
player2ImageTravel(playerImage);
p2value=playerImage;
}
if(playerImage==99)
{
playerImage=41;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==89)
{
playerImage=53;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==76)
{
playerImage=58;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==66)
{
playerImage=45;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==54)
{
playerImage=31;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==43)
{
playerImage=18;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==40)
{
playerImage=3;
player2ImageTravel(playerImage);
p2value=playerImage;
}
else if(playerImage==27)
{
playerImage=5;
player2ImageTravel(playerImage);
p2value=playerImage;
}
}
if(playerImage>=100)
{
//System.out.println("u r win");
gameOver=1;
win(0);
}
}
catch(Exception ignored)
{
}
}
private void imageChange(ActionEvent e)
{
int i,j = 0;
int flag=0;
try{
for( i=0;i<10;i++)
{
for( j=0;j<10;j++)
{
if(e.getSource()==grid[i][j])
{
//System.out.println("Button Found");
//System.out.println("b["+i+"]["+j+"]");
flag=1;
break;
}
//System.out.println(j);
}
if(flag==1)
break;
}
if(imageFlag==0)
{
Thread.sleep(200);
grid[i][j].setIcon(p1);
imageFlag=1;
prevIp1=i;
prevJp1=j;
}
else
{
path=board[prevIp1][prevJp1];
str=Integer.toString(path);
Thread.sleep(200);
icon[prevIp1][prevJp1]=new ImageIcon("images/snakesAndLadders/"+str+".jpg");
Thread.sleep(200);
grid[prevIp1][prevJp1].setIcon(icon[prevIp1][prevJp1]);
grid[i][j].setIcon(p1);
prevIp1=i;
prevJp1=j;
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}