MyToy No.2: ODD PERSON WINS

xiaoxiao2021-03-05  23

/ * * CREATED ON 2004-11-22 * /

/ ** * @Author tinypandabear * * suppose you have to pick one of the three to * Win Some Prize or do some task. A common method is for each of them to flip a * coin. IF One Coin Comes Up Differently Than The Other Two, That Person Wins. * We CAN Make A Game Out of this by Repeating The Contest a Number of Times and * Seeing Who Wins The Most Times. * /

Import java.util. *; import java.io. *;

Class Person Extends Thread {Private Static Random Rand = New Random (); Private PiPedWriter [] OUT; Private PipedReader [] in;

// give every thread a unique ID, for toString () private static int counter = 0; private int number = counter ;

Private int numofgames; private int mywin = 0;

Public Person () {/ * default constructor * /}

PUBLIC PERSON (INT NOG) {Numofgames = Nog; // Two for Other Players, One for Main () Thread Out = New PiPedWriter [3]; for (INT i = 0; i <3; i ) OUT [i] = New pipedwriter ();

Public void setpipedreaderarray (Person [] P) throws oException {IN = New PipedReader [2]; in [0] = new pipedreader (p [(Number 1)% 3] .GetpipedWriter (0)); in [1] = New PipedReader (p [(Number 2)% 3] .GetpipedWriter (1));

Public PiPedWriter getpipedwriter (int index) {return out [index];

Public void Run () {for (int i = 0; i

Public String Tostring () {return "Person" Number "";}}

Public class oddpersonwins {static pipedreader [] readchannel = new supplies [3];

Static Person [] P = New Person [3];

Public static void main (string [] args) throws ioException {// if use args [0] here, outofboundexception shouth; // integer.parseint (args [0]); for (int i = 0; i <3; i ) {p [i] = new person (Rounds); readchannel [i] = new pipedreader (p [i] .Getpipedwriter (2));} // set pipedreader Should Be After ARRAY HAS BEEN Initialized. for (int i = 0; i <3; i ) P [i] .SetpipedReaderarray (P); // Threads Should Start After Initialization Finished. FOR (INT i = 0; i <3; i ) P [i ] .Start (); // Wait Until All threads finished running. FOR (int i = 0; i <3; i ) try {p [i] .join ();} catch (interruptedExcection e) {throw new runtimeException e);} system.out.println ("Total Games:" ROUNDS; for (int i = 0; i <3; i ) {Int Won = (int) readchannel [i] .read (); system. OUT .println ("Player" i "WON:" WON); ROUNDS - = WON;} System.out.println ("Total Ties:" rounds);}} Question Description Source code.

Each thread has 5 PIPEs, two read, three writers, readers, and Writer, and more writer is used to transform to main (), send their own status when the game is completed. The main thread is used for statistics. information.

Each round each thread uses rand.nextint (2) to generate one or 0 or 1 random number, then send two other threads, then read the results of the other two threads from the PIPE, one and their own results Compare, if you are different, then you will win, add one on myWIN, but no matter what you do. When all the rounds end, send MyWIN to the main thread, after the main thread receives the MYWIN sent by the three threads, and the total number of rounds is subtracted to obtain the number of orders.

转载请注明原文地址:https://www.9cbs.com/read-37662.html

New Post(0)