Skip to content

Euler54 tests

package poker;

import java.util.ArrayList;

public class tests {
static char[] cardvalue = {‘2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’T’,’J’,’Q’,’K’,’A’};
static char[] suit = {‘C’,’S’,’H’,’D’};
static int testhand(String hands[])
{
ArrayList hand1 = new ArrayList();
for (int i = 0; i < 5; i++)
{
hand1.add(hands[i]);
}
ArrayList hand2 = new ArrayList();
for (int i = 5; i < 10; i++)
{
hand2.add(hands[i]);
}
int strtest = straight(hand1, hand2);
int flushtest = flush(hand1, hand2);
int fourtest = four(hand1, hand2);
int threetest = three(hand1, hand2);
int fullhousetest = fullhouse(hand1, hand2);
int twopairtest = twopair(hand1, hand2);
int pairtest = pair(hand1, hand2);
int hightest = high(hand1, hand2);
if (flushtest == 1)
{
if (strtest == 1)
{
System.out.println(“Straight Flush!”);
return 1;
}
}
if (flushtest == 2)
{
if (strtest == 2)
{
if (hightest == 1)
{
System.out.println(“Straight Flush!”);
return 1;
}
else
{
return 0;
}
}
}
if (flushtest == -1)
{
if (strtest == -1)
{
return 0;
}
}

if (fourtest == 2)
{
for (int i = 0; i < 5; i++)
{
System.out.print(hand1.get(i)+” “);
}
System.out.print(” “);
for (int i = 0; i < 5; i++)
{
System.out.print(hand2.get(i)+” “);
}
System.out.println();
}
if (fourtest == 1)
{
System.out.println(“Four of a kind!”);
return 1;
}
if (fourtest == -1)
{
return 0;
}
if (fullhousetest == 1)
{
System.out.println(“Full House!”);
return 1;
}
if (fullhousetest == 2)
{
if (threetest == 1)
{
System.out.println(“Full House!”);
return 1;
}
if (threetest == -1)
{
return 0;
}
if (threetest == 2)
{
if (pairtest == 1)
{
System.out.println(“Full House!”);
return 1;
}
else
{
return 0;
}
}
}
if (fullhousetest == -1)
{
return 0;
}
if (flushtest == 1)
{
System.out.println(“Flush!”);
return 1;
}
if (flushtest == 2)
{
if (hightest == 1)
{
System.out.println(“Flush!”);
return 1;
}
else
{
return 0;
}
}
if (flushtest == -1)
{
return 0;
}

if (strtest == 1)
{
System.out.println(“Straight!”);
return 1;
}
if (strtest == -1)
{
return 0;
}
if (strtest == 2)
{
if (hightest == 1)
{
System.out.println(“Straight!”);
return 1;
}
else
{
return 0;
}
}
if (threetest == 1)
{
System.out.println(“Three of a Kind!”);
return 1;
}
if (threetest == -1)
{
return 0;
}
if (threetest == 2)
{
if (hightest == 1)
{
System.out.println(“Three of a Kind!”);
return 1;
}
else
{
return 0;
}
}
if (twopairtest == 1)
{
System.out.println(“two pair!”);
return 1;
}
if (twopairtest == -1)
{
return 0;
}
if (twopairtest == 2)
{
if (pairtest == 1)
{
System.out.println(“two pair!”);
return 1;
}
if (pairtest == -1)
{
return 0;
}
}
if (pairtest == 1)
{
System.out.println(“pair!”);
return 1;
}
if (pairtest == -1)
{
return 0;
}
if (hightest == 1)
{
System.out.println(“High Card!”);
return 1;
}
else
{
return 0;
}

}
static int straight(ArrayList hand1, ArrayList hand2)
{
int a = 0;
ArrayList hand1val = new ArrayList();
ArrayList hand2val = new ArrayList();
for (int i = 8; i>=0; i–)
{
if (hand1val.indexOf(Character.toString(cardvalue[i]))>-1)
{
if (hand1val.indexOf(Character.toString(cardvalue[i+1]))>-1)
{
if (hand1val.indexOf(Character.toString(cardvalue[i+2]))>-1)
{
if (hand1val.indexOf(Character.toString(cardvalue[i+3]))>-1)
{
if (hand1val.indexOf(Character.toString(cardvalue[i+4]))>-1)
{
a = 1;
}

}

}

}

}
if (hand2val.indexOf(Character.toString(cardvalue[i]))>-1)
{
if (hand2val.indexOf(Character.toString(cardvalue[i+1]))>-1)
{
if (hand2val.indexOf(Character.toString(cardvalue[i+2]))>-1)
{
if (hand2val.indexOf(Character.toString(cardvalue[i+3]))>-1)
{
if (hand2val.indexOf(Character.toString(cardvalue[i+4]))>-1)
{
if (a == 1)
{
return 2;
}
else
{
return -1;
}
}

}

}

}

}
}
return a;

}
static int flush(ArrayList hand1, ArrayList hand2)
{
ArrayList hand1suit = new ArrayList();
ArrayList hand2suit = new ArrayList();
for (int i = 0; i<5; i++)
{
hand1suit.add(hand1.get(i).substring(1,2));
hand2suit.add(hand2.get(i).substring(1,2));
}
int j = 0;
int k = 0;
for(int i =0;i<4;i++)
{
if(hand1suit.indexOf(Character.toString(suit[i]))<0)
{
j=j+1;
}
if(hand2suit.indexOf(Character.toString(suit[i]))<0)
{
k=k+1;
}
}
if (j== 3)
{
if (k== 3)
{
return 2;
}
return 1;
}
if (k== 3)
{
return -1;
}
return 0;

}
static int four(ArrayList hand1, ArrayList hand2)
{
ArrayList hand1val = new ArrayList();
ArrayList hand2val = new ArrayList();
int hand1count;
int hand2count;
for (int i = 0; i<5; i++)
{
hand1val.add(hand1.get(i).substring(0,1));
hand2val.add(hand2.get(i).substring(0,1));
}
for (int i = 0; i<2; i++)
{
hand1count = 0;
hand2count = 0;
for (int j = i; j<5; j++)
{
if (hand1val.get(i).equals(hand1val.get(j)))
{
hand1count++;
}
if (hand2val.get(i).equals(hand2val.get(j)))
{
hand2count++;
}
}
if(hand1count >3)
{
if (hand2count>3)
{
// System.out.println(“Both hands are 4 of a kind”);
return 2;
}
return 1;
}
if (hand2count>3)
{
return -1;
}
}
return 0;
}
static int fullhouse(ArrayList hand1, ArrayList hand2)
{
ArrayList hand1valf = new ArrayList();
ArrayList hand2valf = new ArrayList();
for (int i = 0; i<5; i++)
{
hand1valf.add(hand1.get(i).substring(0,1));
hand2valf.add(hand2.get(i).substring(0,1));
}

boolean flag1 = true;
boolean flag2 = true;
for (int i = 0; i < 5; i++)
{
if (hand1valf.lastIndexOf(hand1valf.get(i))==(hand1valf.indexOf(hand1valf.get(i))))
{
flag1 = false;
}
if (hand2valf.lastIndexOf(hand2valf.get(i))==(hand2valf.indexOf(hand2valf.get(i))))
{
flag2 = false;
}

}
if (flag1 == true)
{
if (flag2 == true)
{
return 2;
}
else
{
return 1;
}
}
if (flag2 == true)
{
return -1;
}
return 0;
}
static int three(ArrayList hand1, ArrayList hand2)
{ ArrayList hand1val = new ArrayList();
ArrayList hand2val = new ArrayList();
int hand1count;
int hand2count;
for (int i = 0; i<5; i++)
{
hand1val.add(hand1.get(i).substring(0,1));
hand2val.add(hand2.get(i).substring(0,1));
}
for (int i = 0; i<3; i++)
{
hand1count = 0;
hand2count = 0;
for (int j = i; j<5; j++)
{
if (hand1val.get(i).equals(hand1val.get(j)))
{
hand1count++;
}
if (hand2val.get(i).equals(hand2val.get(j)))
{
hand2count++;
}
}
if(hand1count >2)
{
if (hand2count>2)
{
// System.out.println(“Both hands are 4 of a kind”);
return 2;
}
return 1;
}
if (hand2count>2)
{
return -1;
}
}
return 0;
}
static int twopair(ArrayList hand1, ArrayList hand2)
{
ArrayList hand1val = new ArrayList();
ArrayList hand2val = new ArrayList();
int hand1count;
int hand1sum;
int hand2count;
int hand2sum;
for (int i = 0; i<5; i++)
{
hand1val.add(hand1.get(i).substring(0,1));
hand2val.add(hand2.get(i).substring(0,1));
}
hand1sum = 0;
hand2sum = 0;
for (int j = 12; j>=0; j–)
{
hand1count = 0;
hand2count = 0;
for (int i = 0; i<5; i++)
{
// System.out.println(hand1val.get(i)+” tested on “+String.valueOf(cardvalue[j]));
if (hand1.get(i).substring(0,1).equals(String.valueOf(cardvalue[j])))
{
hand1count++;
if (hand1count == 2)
{
hand1sum++;
}

}
if (hand2.get(i).substring(0,1).equals(String.valueOf(cardvalue[j])))
{
hand2count++;
if (hand2count==2)
{
hand2sum++;
}
}
}
}
if(hand1sum==2)
{
if (hand1sum > hand2sum)
{
// System.out.println(“player 1 has two pair!”);

return 1;
}
if (hand1sum == hand2sum)
{
// System.out.println(“both players have two pair!”);

return pair(hand1, hand2);
}
}
if(hand2sum==2)
{
// System.out.println(“player 2 has two pair!”);
return -1;
}
return 0;
}
static int pair(ArrayList hand1, ArrayList hand2)
{
ArrayList hand1val = new ArrayList();
ArrayList hand2val = new ArrayList();
int hand1count;
int hand2count;
for (int i = 0; i<5; i++)
{
hand1val.add(hand1.get(i).substring(0,1));
hand2val.add(hand2.get(i).substring(0,1));
}
for (int j = 12; j>=0; j–)
{
hand1count = 0;
hand2count = 0;
for (int i = 0; i<5; i++)
{
// System.out.println(hand1val.get(i)+” tested on “+String.valueOf(cardvalue[j]));
if (hand1.get(i).substring(0,1).equals(String.valueOf(cardvalue[j])))
{
hand1count++;
}
if (hand2.get(i).substring(0,1).equals(String.valueOf(cardvalue[j])))
{
hand2count++;
}
}
if(hand1count > 1)
{
if (hand2count == hand1count)
{
// System.out.println(“Both hands have a pair”);
return 2;
}
if (hand1count > hand2count)
{
// System.out.println(“player 1 has a pair!”);
return 1;
}
}
if (hand2count > 1)
{
// System.out.println(“player 2 has a pair!”);

return -1;
}
}

return 0;
}
static int high(ArrayList hand1, ArrayList hand2)
{
ArrayList hand1val = new ArrayList();
ArrayList hand2val = new ArrayList();
int hand1count;
int hand2count;
for (int i = 0; i<5; i++)
{
hand1val.add(hand1.get(i).substring(0,1));
hand2val.add(hand2.get(i).substring(0,1));
}
for (int j = 12; j>=0; j–)
{
hand1count = 0;
hand2count = 0;
for (int i = 0; i<5; i++)
{
// System.out.println(hand1val.get(i)+” tested on “+String.valueOf(cardvalue[j]));
if (hand1.get(i).substring(0,1).equals(String.valueOf(cardvalue[j])))
{
hand1count++;
}
if (hand2.get(i).substring(0,1).equals(String.valueOf(cardvalue[j])))
{
hand2count++;
}
}
if(hand1count > hand2count)
{
// System.out.println(“player 1 has a high card!”);
return 1;
}
else if (hand2count > hand1count)
{
// System.out.println(“player 2 has a high card!”);
return 0;
}
else if (hand1count > 0)
{
// System.out.println(“cards match!”);
}
}

return 0;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: