Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

Java, Point system for a Yahtzee program

So i am working on a school project and the project is to make a simplified yahtzee program with two of the yahtzee point systems, ex small ladder or three of a kind and so far i have made small ladder (1,2,3,4,5) and big ladder (2,3,4,5,6) to work but i have 2 questions. 1. How can i make a point system for three of a kind? 2. A part of the project is to make a category if you dont get one of the implied point systems, ex if you get (1,1,4,5,6) then the user is gonna get the option to choose which number he wants to pick for maximum points. I know i cant explain this very well since im swedish and my english isnt the best. But here is my code:

import javax.swing.*;
import java.util.*;


public class Projekt1 {

public static void main(String[] args) {

char fortsatta = 'j';
boolean ja;
do{
JOptionPane.showMessageDialog(null, "V?lkommen till Johans Yatzy");
int starta = JOptionPane.showConfirmDialog(null, "Vill du starta spelet?",  "Spela igen",
                                                JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE);

if(starta == JOptionPane.YES_OPTION ){
 JOptionPane.showMessageDialog(null, "Du har nu startat spelet");


int[] tar = new int[5];

for(int i=0; i<5; i++){
tar[i] = (int)(Math.random()*6+1);
}

String output = "";
java.util.Arrays.sort(tar);

for (int i = 0; i < tar.length; i++) {
output = output + tar[i] + "	";
}

JOptionPane.showMessageDialog(null, "Du har nu kastat dina t?rningar och kasten blev f?ljande: " + output);


if (tar[0] == 1 && tar[1] == 2 && tar[2]==3 && tar[3] == 4 && tar[4] == 5) {
  JOptionPane.showMessageDialog(null, "Grattis, du fick liten stege som ?r v?rt 15 po?ng");
}

else if (tar[0] == 2 && tar[1] == 3 && tar[2]==4 && tar[3] ==5 && tar[4] ==6) {
  JOptionPane.showMessageDialog(null, "Grattis, du fick stor stege som ?r v?rt 20 po?ng");
}

else{
  JOptionPane.showMessageDialog(null, "T?rningskastet resulterade i varken liten stege eller stor stege");  
}
}
else if(starta == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null, "Programmet kommer nu att st?ngas ner");
System.exit(0);
}


String startaom;
startaom = JOptionPane.showInputDialog("Vill du spela igen? Skriv J f?r att spela igen");
fortsatta = startaom.charAt(0); 
}while(fortsatta == 'j');

}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know you're only doing a simplified version of Yahtzee, so first, here is the simple way to check for three-of-a-kind:

if ((tar[0] == tar[1] && tar[1] == tar[2]) ||
    (tar[1] == tar[2] && tar[2] == tar[3]) ||
    (tar[2] == tar[3] && tar[3] == tar[4])) {
    // found 3 of a kind
}

For a more full-blown version, there are a lot of points for various combinations of multiple-of-a-kind in the lower section:

  • Three-Of-A-Kind
  • Four-Of-A-Kind
  • Full House (Three-Of-A-Kind + Two-Of-A-Kind)
  • Yahtzee (Five-Of-A-Kind)

and for the extended Yatzy version (not Yahtzee):

  • One-Pair (Two-Of-A-Kind)
  • Two-Pair (Two-Of-A-Kind + Two-Of-A-Kind)

and for the upper section you need to count how many of a certain number is present.

So, you should start by doing the counts-by-number:

int[] count = new int[6];
for (int i = 0; i < 5; i++)
    count[tar[i]-1]++;

Now find the top two counts:

int topIdx1 = 0, topIdx2 = 0;
for (int i = 1; i < 6; i++)
    if (count[i] > count[topIdx1]) {
        topIdx2 = topIdx1;
        topIdx1 = i;
    } else if (count[i] > count[topIdx2]) {
        topIdx2 = i;
    }

Now you can easily find your combination (and the points if doing Yatzy):

if (count[topIdx1] == 5) {
    // Yahtzee
} else if (count[topIdx1] == 4) {
    // Four-Of-A-Kind       yatzyPoints = 4*(topIdx1+1)
} else if (count[topIdx1] == 3) {
    if (count[topIdx2] == 2) {
        // Full House       yatzyPoints = 3*(topIdx1+1) + 2*(topIdx2+1)
    } else {
        // Three-Of-A-Kind  yatzyPoints = 3*(topIdx1+1)
    }
} else if (count[topIdx1] == 2) {
    if (count[topIdx2] == 2) {
        // Two-Pair         yatzyPoints = 2*(topIdx1+1) + 2*(topIdx2+1)
    } else {
        // One-Pair         yatzyPoints = 2*(topIdx1+1)
    }
}

And for the upper section, the points are easily calculated:

points = number * count[number-1]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...