classMain.java
public class classMain
{
public static classCall classCallObject;
public static void main(String[] args)
{
classCallObject = new classCall();
classCallObject.calling();
}
}
classCall.java
import java.awt.CardLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class classCall extends JFrame
{
public JFrame frame;
public JPanel cards;
public CardLayout cardlayout = new CardLayout();
public static classEngine engObject = new classEngine();
public void calling()
{
frame = new JFrame();
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.setTitle("Cricket Game 2012");
frame.setSize(1024, 740);
frame.setResizable(false);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui guiObject = new gui();
frame.add(guiObject.totalScore);
}
}
gui.java
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class gui extends JFrame
{
public JLabel totalScore;
public gui()
{
classCall.engObject.gameEngine();
totalScore = new JLabel(String.format("Total is %d - %d", classCall.engObject.getTotal(), classCall.engObject.getWickets()));
totalScore.setLayout(new FlowLayout());
}
}
classEngine.java
import java.util.Random;
public class classEngine
{
public int[][] Overs = new int[20][6];
public static int total=0;
public static int wickets=0;
public void gameEngine()
{
for (int over=1; over<Overs.length;over++)
{
for (int ball=1; ball<Overs[over].length; ball++)
{
Overs[over][ball] = batsmanNormalNormal();
total=total + Overs[over][ball];
}
}
}
public int batsmanNormalNormal()
{
int x=0;
int randomNumber = 0;
Random randomObject = new Random();
randomNumber=randomObject.nextInt(100)+1;
if (randomNumber<=1 && randomNumber>=10)
x=4;
else if (randomNumber<=11 && randomNumber>=13)
{
x=0;
wickets++;
}
else if (randomNumber<=14 && randomNumber>=17)
x=1;
else if (randomNumber<=18 && randomNumber>=19)
x=1;
else if (randomNumber<=20 && randomNumber>=30)
x=2;
else if (randomNumber<=31 && randomNumber>=55)
x=1;
else if (randomNumber<=56 && randomNumber>=96)
x=0;
else if (randomNumber<=97 && randomNumber>=98)
x=6;
else if (randomNumber<=99 && randomNumber>=100)
x=3;
return x;
}
public int getTotal()
{
return total;
}
public int getWickets()
{
return wickets;
}
}
I have tried to create a java code which stimulates the game of cricket.
I know many of you may not know this game. Basically, what I want to do is randomly generate numbers (weighted probability – not completely random) and add them to a static int called total.
I want to display the Total using the JLabel.
But for some reason, the total doesnt update. It says 0-0.
It should give other results like 140 or 200 because i have coded it to add randomly generated numbers to the total approx. 120 times.
I think I may be missing something.
I think it is something apparent which my blank brain cannot read atm.
Can somebody plz help me with this?
Thanks
Make sure to check that greater than and lesser than signs are at the right place.
In this program the if and else if statements never get executed because the conditions that I have put there are never true!
Thats why the total score is not updated.