Analysis of Small game programs

Source: Internet
Author: User

Q: this is a question I asked when I went to elementary school. He said that when I built a house in the school, a bunch of migrant workers were playing this game together and there were three piles of stones, there are 3, 5, and 7 respectively. The game rule is to take two people in turn. Each person can only take one pile at a time, and each time can only take one to three, whoever takes the last stone gives the other party a dollar.

 


Package COM. HPU. bai; import Java. util. role; public class cobble {public static void main (string [] ARGs) {cobble c = new cobble (); C. print (); C. getcobble () ;}int [] arr = {3, 5, 7}; int n = 15; int COUNT = 0; counter input = new partition (system. in); Public int total () {int m = 0; For (INT I = 0; I <arr. length; I ++) {M + = arr [I];} return m;} public void print () {for (INT I = 0; I <arr. length; I ++) {system. out. println ("no" + (I + 1) + "remaining" + arr [I] + "##") ;}} public void player (int B) {If (B % 2 = 1) {system. out. println ("the first person took the stone");} else {system. out. println ("this second person has taken stones") ;}} public void getcobble () {count ++; If (n> 1) {player (count); system. out. println ("Enter in which stone you want to take the stone"); int I = input. nextint ()-1; system. out. println ("Enter the number of stones you want to fetch in this pile"); int M = input. nextint (); If (M> 3) {system. out. println ("your input is incorrect. Please enter it again"); count --; getcobble ();} else {arr [I] = arr [I]-m; if (ARR [I] <0) {system. out. println ("the stone is almost gone, or you should take less points, or you should change a pile"); arr [I] = arr [I] + m; count --;} print (); n = total (); getcobble () ;}} else {If (count % 2 = 1) {system. out. println ("don't take it, you have already lost 1st players");} else {system. out. println ("don't take it, you have already lost 2nd players ");}}}}

The key to program implementation is to have a variable similar to a pointer to control who should take it. Of course, after learning the thread well, this program can be further improved. Java beginner QQ: 623687677. You can communicate with each other or run the code.

Analysis of Small game programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.