Basically I'm making a domino game in Java; however I'm pretty new and can't figure out how to shuffle the dominoes up using arrays and buffered Images.
This code here is setting up the arrays for each hand and an array with each domino piece. Keep in mind that I am not using an AI for this domino game;
the program runner will just play as each player.
static String [] hand1 = new String[7];
static String [] hand2 = new String[7];
static String [] hand3 = new String[7];
static String [] hand4 = new String[7];
static String[] allDominoes = {"00","01","02","03","04","05","06","11","12","13","14","15","16","22","23",
"24","25","26","33","34","35","36","44","45","46","55","56","66"};
This is what I tried to use to shuffle the dominoes and put them into each hand array:
public static void newRound() // start a new round; shuffle the dominoes; hand them out
{
Arrays.fill(hand1, null);
Arrays.fill(hand2, null);
Arrays.fill(hand3, null);
Arrays.fill(hand4, null);
String[] shuffledDominos = new String[28];
List<String> shuffled = Arrays.asList(allDominoes);
Collections.shuffle(shuffled);
shuffled.toArray(shuffledDominos);
int pos = 0;
for (int j = 0; j < 7; j++)
{
hand1[pos] = shuffledDominos[j];
pos++;
}
pos = 0;
for (int y = 7; y < 14; y++)
{
hand2[pos] = shuffledDominos[y];
pos++;
}
pos = 0;
for (int x = 14; x < 21; x++)
{
hand3[pos] = shuffledDominos[x];
pos++;
}
pos = 0;
for (int z = 21; z < 28; z++)
{
hand4[pos] = shuffledDominos[z];
pos++;
}
However I think this part is completely wrong and there must be a better way of doing this since I'm not sure how to actually proceed from here on out.
This is assigning each domino Image the domino pngs.
public DominoPanel()
{
try
{
dom00 = ImageIO.read(DominoPanel.class.getResource("/Image/00.png"));
dom01 = ImageIO.read(DominoPanel.class.getResource("/Image/01.png"));
dom02 = ImageIO.read(DominoPanel.class.getResource("/Image/02.png"));
dom03 = ImageIO.read(DominoPanel.class.getResource("/Image/03.png"));
dom04 = ImageIO.read(DominoPanel.class.getResource("/Image/04.png"));
dom05 = ImageIO.read(DominoPanel.class.getResource("/Image/05.png"));
dom06 = ImageIO.read(DominoPanel.class.getResource("/Image/06.png"));
dom11 = ImageIO.read(DominoPanel.class.getResource("/Image/11.png"));
dom12 = ImageIO.read(DominoPanel.class.getResource("/Image/12.png"));
dom13 = ImageIO.read(DominoPanel.class.getResource("/Image/13.png"));
dom14 = ImageIO.read(DominoPanel.class.getResource("/Image/14.png"));
dom15 = ImageIO.read(DominoPanel.class.getResource("/Image/15.png"));
dom16 = ImageIO.read(DominoPanel.class.getResource("/Image/16.png"));
dom22 = ImageIO.read(DominoPanel.class.getResource("/Image/22.png"));
dom23 = ImageIO.read(DominoPanel.class.getResource("/Image/23.png"));
dom24 = ImageIO.read(DominoPanel.class.getResource("/Image/24.png"));
dom25 = ImageIO.read(DominoPanel.class.getResource("/Image/25.png"));
dom26 = ImageIO.read(DominoPanel.class.getResource("/Image/26.png"));
dom33 = ImageIO.read(DominoPanel.class.getResource("/Image/33.png"));
dom34 = ImageIO.read(DominoPanel.class.getResource("/Image/34.png"));
dom35 = ImageIO.read(DominoPanel.class.getResource("/Image/35.png"));
dom36 = ImageIO.read(DominoPanel.class.getResource("/Image/36.png"));
dom44 = ImageIO.read(DominoPanel.class.getResource("/Image/44.png"));
dom45 = ImageIO.read(DominoPanel.class.getResource("/Image/45.png"));
dom46 = ImageIO.read(DominoPanel.class.getResource("/Image/46.png"));
dom55 = ImageIO.read(DominoPanel.class.getResource("/Image/55.png"));
dom56 = ImageIO.read(DominoPanel.class.getResource("/Image/56.png"));
dom66 = ImageIO.read(DominoPanel.class.getResource("/Image/66.png"));
}
catch(Exception E)
{
}
I know I probably didn't explain this well so I will try to summarize it here as best as I can. I need to be able to assign a random domino to each player and then print them out in a JPanel using buffered Images.
Related
I am using GraphView library code which contains DataPoint[] Array.
My code also contains a database named olddb which I use to insert the array length in the code below.
The first 2 for loops are for adding the values in the database into an array and the third for loop is to get the values out from the arrays arrayWeight and arrayId and add them to DataPoint[] array.
Though something seems to go wrong, after I run the program it just crashes, can anyone figure out what is causing the application to crash?
private DataPoint[] getDataPoint() {
if (olddb.check()) {
List<oldDetails> details = olddb.getDetails();
double[] arrayWeight = new double[olddb.getDetailsCount()];
int[] arrayId = new int[olddb.getDetailsCount()];
for (oldDetails cn : details) { //Adding weights and id of all time to an array.
double num = cn.getWeight();
int id = cn.getId();
for (int i = arrayWeight.length; i > 0; i--) {
arrayWeight[i] = num;
arrayId[i] = id;
}
}
DataPoint[] dp = new DataPoint[olddb.getDetailsCount()];
for (int i = 0; i < arrayId.length; i++) {
for (int j = 0; j < arrayWeight.length; j++)
dp[i] = new DataPoint(i, arrayWeight[j]);
}
return dp;
}
else {
DataPoint[] dp = new DataPoint[]{
new DataPoint(0, 0)
};
return dp;
}
}
Here is an example for working code of DataPoint array:
private DataPoint[] getDataPoint(){
DataPoint[] dp = new DataPoint[]{
new DataPoint(0,1),
new DataPoint(2,5),
new DataPoint(5,5),
new DataPoint(7,4)
};
return dp;
}
EDIT:
here is the error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.none.myapplication, PID: 3881
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.none.myapplication/com.none.myapplication.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM oldDetails
Database
You can fix it by replacing all 3 calls to olddb.getDetailsCount()
with
int count = olddb.getDetailsCount();
then:
double[] arrayWeight = new double[count];
int[] arrayId = new int[count];
DataPoint[] dp = new DataPoint[count];
However you need to understand the issue:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM oldDetails is the error, which happens inside of DatabaseHandlerOld.getDetailsCount line 141
Android SQLite DB When to Close
Well after some researches and some of Blundell help, I figured out the problem was indeed the close(); method in my database.
Though the solution was to change the getDetailsCount() function to this:
public long QueryNumEntries()
{
SQLiteDatabase db = this.getReadableDatabase();
return DatabaseUtils.queryNumEntries(db, TABLE_OLDDETAILS);
}
and in my main activity file:
private DataPoint[] getDataPoint() {
if (olddb.check()) {
List<oldDetails> details = olddb.getDetails();
long c = olddb.QueryNumEntries();
int count = (int) c;
Log.d("Count", "equals: " + count);
double[] arrayWeight = new double[count];
int[] arrayId = new int[count];
for (oldDetails cn : details) { //Adding weights and id of all time to an array.
double num = cn.getWeight();
int id = cn.getId();
Log.d("num", "equals: " + num);
Log.d("id", "equals: " + id);
for (int i = count; i < 0; i--) {
arrayWeight[i] = num;
arrayId[i] = id;
}
}
DataPoint[] dp = new DataPoint[count];
for (int i = 0; i < arrayId.length; i++) {
for (int j = 0; j < arrayWeight.length; j++) {
dp[i] = new DataPoint(arrayId[i], arrayWeight[j]);
Log.d("ArrayWeight", "equals: " + arrayWeight[j]);
Log.d("ArrayId", "equals: " + arrayId[i]);
}
}
return dp;
}
DataPoint[] dp = new DataPoint[]{
new DataPoint(0, 0)
};
return dp;
}
I have a project for school to take my current pacman program and have pacman traverse the 2-d char array to find a pattern to complete the maze.
I am trying to use a stack and add specific index of my maze to the stack so when pacman reaches the index it pops off and he chooses a direction randomly to another index and so forth until all dots are eaten. My issue is taking my 2d array and finding the index values of the intersections and then adding them to a stack.
I am new to java and not really sure how to approach this I posted my maze I have and some code that will use the stack to find the sequence. Thanks in advanced.
private void create() {
String[] tier = new String[tall];
tier[0] = "|======================================|";
tier[1] = "|......................................|";
tier[2] = "|.====.==========================.====.|";
tier[3] = "|.||||............................||||.|";
tier[4] = "|.====.==========================.====.|";
tier[5] = "|......................................|";
tier[6] = "|.====================================.|";
tier[7] = "|......................................|";
tier[8] = "|.====.======.====----====.======.====.|";
tier[9] = "|.||||........|| ||........||||.|";
tier[10] = "|.||||.======.|| ||.======.||||.|";
tier[11] = "|.||||........|| ||....... ||||.|";
tier[12] = "|.====.======.============.======.====.|";
tier[13] = "|......................................|";
tier[14] = "|.===========..............===========.|";
tier[15] = "|.............===== =====.............|";
tier[16] = "|.=======.===.||$ $||.===.=======.|";
tier[17] = "|.|||||||.===.===== =====.===.|||||||.|";
tier[18] = "|.|||||||......................|||||||.|";
tier[19] = "|.|||||||.====================.|||||||.|";
tier[20] = "|.=======......................=======.|";
tier[21] = "|.==......====================......==.|";
tier[22] = "|.==.====.======........======.====.==.|";
tier[23] = "|....====........======........====....|";
tier[24] = "|======================================|";
for (int i = 0; i < tall; i++) {
array[i] = tier[i].toCharArray();
}
}
public boolean isPattern(Spot b, Spot e) {
Stack<Spot> ss = new Stack<Spot>();
Spot topPost, nextPost;
allOld();
ss.push(b);
markOld(b);
topPost = ss.peek();
while (!ss.empty() && (topPost.compareTo(e) != 0)) {
nextPost = getNextSpot(topPost);
if (nextPost == null) {
ss.pop();
} else {
ss.push(nextPost);
markOld(nextPost);
}
topPost = ss.peek();
}
if (ss.empty()) {
return false;
} else {
return true;
}
}
public void index(int x, int y, int dx, int dy){
x=array.length;
y=array[x].length;
for( int i=0;i <x;i++){
for(int j =0; j<y;j++){
if(array[i].get(i))//this should determine if its aninersection and then add to the stack
}
}
}
}
the last method is where i am getting stuck. I dont know how to get the specific index values. I guess i can enter them manually but I am sure there is an easier way. Please comment if need more context on the problem and Thanks again.
I try to retreive information from catia usig com4j. Some methods require to pass an array in argument to retreive information but the array is never populated. In this example is to get coordinate from a point in catia.
The declaration of the method generated by com4j
public interface Point extends com4j.catia.HybridShape {
...
void getCoordinates(java.lang.Object[] oCoordinates);
...
}
my code to get the coordinate
public static void testGetPointCoordinates() {
String catiafile="E:\\test.CATPart";
Application app =null;
app = COM4J.createInstance(Application.class, "CATIA.Application");
Documents documents = app.documents();
Document oDocument = (Document) documents.open(new Holder<>(catiaFile.getAbsolutePath()));
PartDocument partDocument = oDocument.queryInterface(PartDocument.class);
Part part = partDocument.part();
Factory HSFactory = part.hybridShapeFactory();
HybridShapeFactory HSF = HSFactory.queryInterface(HybridShapeFactory.class);
HybridBodies hbodies = part.hybridBodies();
int n = hbodies.count();
for (int i = 1; i <= n; i++) {
HybridBody hbody = null;
hbody = hbodies.item(i);
int nbitems = hbody.hybridShapes().count();
for (int j = 1; j <= nbitems; j++) {
String name = hbody.hybridShapes().item(j).name();
System.out.println("name=" + name);
//Object tab[]=new Object[3];
if (name.startsWith("Point.12")) {//true point
HybridShape hs = hbody.hybridShapes().item(j);
Reference reference = part.createReferenceFromObject(hs);
HybridShapePointCoord p3 = hs.queryInterface(HybridShapePointCoord.class);
//works
System.out.println("point name = " + p3.name());
System.out.println(p3.y().value());//display 50.0
//doesn't work
Variant tab[] = new Variant[3];
for (int k = 0; k < 3; k++) {
Variant variant = new Variant();
variant.set(k);
tab[k] = variant;
}
p3.getCoordinates(tab);
System.out.println(tab[1].getParseableString()); //display 1 (value not modified)
//doesn't work
tab = new Variant[3];
for (int k = 0; k < 3; k++) {
Variant variant = new Variant(Variant.Type.VT_EMPTY);//tested with VT_R4 VT_R8,...
tab[k] = variant;
}
System.out.println(tab[1].getJavaCode()); //display null (Don't know how to print VT_EMPTY as an Java literal)
//doesn't work with this other solution but ok in VBA
tab = new Variant[3];
//Variant v = new Variant(Type.VT_EMPTY);
tab[0] = new Variant(Variant.Type.VT_EMPTY);
tab[1] = new Variant(Variant.Type.VT_EMPTY);
tab[2] = new Variant(Variant.Type.VT_EMPTY);
HybridShapePointExplicit point = HSF.addNewPointDatum(reference);
point.getCoordinates(tab);
System.out.println(tab[1].doubleValue() + " " + tab[2].toString()); //display 0
//doesn't work
//crash JVM
// tab = new Variant[3];
// p3.getCoordinates(tab);
break;
}
}
}
}
the code below works in VBA with CATIA
Dim P1
Dim coordonnees(2)
Dim coordonnees2(100, 3)
coordonnees(0) = 0
coordonnees(1) = 0
coordonnees(2) = 0
Set P1 = HSF.AddNewPointDatum(hbody.HybridShapes.Item(i))
'fonction to get coordinates
P1.GetCoordinates coordonnees
'set name and coordinates
coordonnees2(Y, 0) = hbody.HybridShapes.Item(i).name
coordonnees2(Y, 1) = Round(coordonnees(0), 3)
coordonnees2(Y, 2) = Round(coordonnees(1), 3)
coordonnees2(Y, 3) = Round(coordonnees(2), 3)
I'm not familiar with com4j, but Point.GetCoordinates is a restricted method:
.
The solution to this problem in VB is you need to create a new variant and set it equal to p3. Then, call GetCoordinates from the variant's context. Intellisense will not work well on the variant, but the call to GetCoordinates will work.
In VB the code would look something like this:
...
Dim p3 As Variant
Dim arrayOfCoord(2) As Variant
Set p3 = hybridShapePointCoord1 'set variant = strongly typed point object
p3.GetCoordinates arrayOfCoord
...
In your case, non-VB, your code might look like this
...
Object p3Obj; //used in vb:Variant p3Var;
p3Obj = p3;
p3Obj.GetCoordinates(tab);//Intellisense will not work, but the call to this method should
...
Let me know if this works.
I had to make a Temp array to keep resizing the array list if the user decides to keep adding items to the cart, but my Temp array works until I try to add 3 different items to the cart.
I was instructed to do it this way instead of an array list to show the difficulty of arrays.
orderProduct [productCount] = aProduct;
orderQuantity [productCount] = aQuantity;
}
}
You forgot to increase productCount when there is already a product in the cart.
Moreover, you can just set the product and quantity array to the temp arrays instead of copying back.
orderProduct = tempOrderedProducts;
orderQuantity = tempOrderedQuantity;
Because you forgot productCount++ after resizing the array.
The following code will work:
public void setOrderProduct(Product aProduct, int aQuantity) {
if (productCount == 0) {
orderProduct[0] = aProduct;
orderQuantity[0] = aQuantity;
} else {
Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
for (int i = 0; i < orderProduct.length; i++) {
tempOrderedProducts[i] = orderProduct[i];
tempOrderedQuantity[i] = orderQuantity[i];
}
orderProduct = new Product[tempOrderedProducts.length];
orderQuantity = new int[tempOrderedQuantity.length];
for (int i = 0; i < orderQuantity.length; i++) {
orderProduct[i] = tempOrderedProducts[i];
orderQuantity[i] = tempOrderedQuantity[i];
}
orderProduct[productCount] = aProduct;
orderQuantity[productCount] = aQuantity;
productCount++; //you forgot this
}
}
What's more, there is a simple way to deal with array copy:
public void setOrderProduct(Product aProduct, int aQuantity) {
if (productCount == 0) {
orderProduct[0] = aProduct;
orderQuantity[0] = aQuantity;
} else {
Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
//System.arraycopy is more convenient and efficient
System.arraycopy(orderProduct, 0, tempOrderedProducts, 0, orderProduct.length);
System.arraycopy(orderQuantity, 0, tempOrderedQuantity, 0, orderQuantity.length);
//you don't need to copy back, just re-assign the reference
orderProduct = tempOrderedProducts;
orderQuantity = tempOrderedQuantity;
orderProduct[productCount] = aProduct;
orderQuantity[productCount] = aQuantity;
productCount++;
}
}
I'm working on a programm in which I want my object "this" will be an array of Point but I have this error when I run the programm and I'm not understand why.
Error --> DouglasPeucker.
My programm :
public class DouglasPeucker {
private double epsilon;
protected Point[] coinImage;
public DouglasPeucker(Point [] tab) {
this.coinImage = new Point[tab.length];
for(int i = 0; i < this.coinImage.length; i++) {
double abscisse = tab[i].getX();
double ordonnee = tab[i].getY();
System.out.println(abscisse + " " + ordonnee);
this.coinImage[i].setX(abscisse);
this.coinImage[i].setY(ordonnee);
}
}
You're never assigning a value to coinImage[i], so it will have its default value of null, which you're the dereferencing. You need something like:
for(int i = 0; i < this.coinImage.length; i++) {
double abscisse = tab[i].getX();
double ordonnee = tab[i].getY();
System.out.println(abscisse + " " + ordonnee);
this.coinImage[i] = new Point();
this.coinImage[i].setX(abscisse);
this.coinImage[i].setY(ordonnee);
}
Or preferrably, IMO:
for (int i = 0; i < this.coinImage.length; i++) {
// I'm assuming Point has a sensible constructor here...
coinImage[i] = new Point(tab[i].getX(), tab[i].getY());
// Insert the diagnostics back in if you really need to
}