Bug 616 - Want to implement state pattern for player module
Summary: Want to implement state pattern for player module
Status: CONFIRMED
Alias: None
Product: CS3343 Chinese Checker
Classification: Unclassified
Component: Algorithm Component (show other bugs)
Version: unspecified
Hardware: All All
: --- enhancement
Assignee: yutongmen2-c
URL:
Depends on:
Blocks:
 
Reported: 2021-11-29 10:33 HKT by yutongmen2-c
Modified: 2021-11-29 10:33 HKT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description yutongmen2-c 2021-11-29 10:33:03 HKT
Steps To Reproduce:

Add toString() method to the Player Interface



Actual Output:

Compilation Error



Expected Output:

Player 1 and 2 alternatively showed.
Comment 1 yutongmen2-c 2021-11-29 10:33:35 HKT
Previous code:

private static Player turnPlayer = (Player) new PlayerOne();

public static void addTurn(){

    	if(turnPlayer==PlayerOne()) {

    		turnPlayer = (Player) new PlayerTwo();

    	}

    	else if(turnPlayer==PlayerTwo()) {

    		turnPlayer = (Player) new PlayerOne();

    	}

        turn++;

}



Modified code:

private static Player turnPlayer = (Player) new PlayerOne();

 public static void addTurn(){

    	if(turnPlayer.toString()=="Player 1") {

    		turnPlayer = (Player) new PlayerTwo();

    	}

    	else if(turnPlayer.toString()=="Player 2") {

    		turnPlayer = (Player) new PlayerOne();

    	}

        turn++;

    }