java - Uno Project: Making sure that a illegal plays return correctly? -
i writing chunk of program play uno other classes. uno project finished, need sure part checks make sure on moves after wild card played, play either legal card or return -1. here code:
import java.util.*; public class alexal_unoplayer implements unoplayer { public int play(list<card> hand, card upcard, color calledcolor, gamestate state) { int play = -1; boolean havewild = false; boolean matcheswildcall = false; int indexofwild = 0; //turn number of cards players holding ints later use int[] array = state.getnumcardsinhandsofupcomingplayers(); int playernext = array[0]; int playertwonext = array[1]; int playerbefore = array[2]; color upcardcolor = upcard.getcolor(); for(int = 0; < hand.size(); i++) { //see if have wilds if(hand.get(i).getrank().equals(rank.wild) || hand.get(i).getrank().equals (rank.wild_d4)) { havewild = true; indexofwild = i; } //set upcard color calledcolor if wild or wild_d4 played if (upcard.getrank().equals(rank.wild) || upcard.getrank().equals(rank.wild_d4)) { upcardcolor = calledcolor; } //always play card matching rank of upcard, if possible, or play first in hand matches color if(hand.get(i).getcolor().equals(upcardcolor)) { if(hand.get(i).getnumber() == upcard.getnumber()) { play = i; } } //if cornered(no matching number or color), play wild else if(havewild == true) { play = indexofwild; } //hold reverse cards until person next after me has less cards person before me if(hand.get(i).getrank().equals(rank.reverse) && playernext < playerbefore) { play = i; } //play skips when person next me has less cards me if((hand.get(i).getrank().equals(rank.skip) || hand.get(i).getrank().equals(rank.draw_two)) && playernext < hand.size()) { play = i; } } return play; } public color callcolor(list<card> hand) { //strategy: change color 1 i'm holding of color changeto = color.green; int numblues = 0; int numgreens = 0; int numreds = 0; int numyellows = 0; //find out how many of each color i'm holding for(int = 0; < hand.size(); i++) { if(hand.get(i).getcolor().equals(color.blue)) { numblues++; } else if(hand.get(i).getcolor().equals(color.red)) { numreds++; } else if(hand.get(i).getcolor().equals(color.green)) { numgreens++; } else if(hand.get(i).getcolor().equals(color.yellow)) { numyellows++; } } //find out i'm holding of , call color //if no majority, return favorite color(green) if(numblues > numreds && numblues > numgreens && numblues > numyellows) { changeto = color.blue; } else if(numreds > numblues && numreds > numgreens && numreds > numyellows) { changeto = color.red; } else if(numgreens > numblues && numgreens > numyellows && numgreens > numreds) { changeto = color.green; } else if(numyellows > numblues && numyellows > numgreens && numyellows > numreds) { changeto = color.yellow; } else { changeto = color.green; } return changeto; } }
for reason, output telling me this:
you given hand: 0. g7 1. g5 2. g+2 , card was: w , called color was: yellow , (wrongly) returned 2. valid plays have included: -1
can provide insight on why getting error , how fix it? appreciated!
based on code, hard wrong implementation.
did try step-by-step debugging of play function? alternately, beneficial instrument code more trace statements better figure out issue might occurring.
for example:
every play variable gets assigned value, print value console context of in code.
if(hand.get(i).getnumber() == upcard.getnumber()) { play = i; system.out.println("number match step: play = " + play ); // can add such line }
this should give idea when play's value getting mutated 2 unexpectedly.
just clutching @ straws here, expected values of getcolor , getnumber wild cards.
in case have them set green or 2 respectively, might explain output seeing because of following fragment of code.
//always play card matching rank of upcard, if possible, or play first in hand matches color if(hand.get(i).getnumber() == upcard.getnumber()) { play = i; } else if(hand.get(i).getcolor() == upcardcolor) { play = i; }
Comments
Post a Comment