-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.go
More file actions
66 lines (59 loc) · 1.66 KB
/
Player.go
File metadata and controls
66 lines (59 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Player.go contains the super struct which defines players of all types.
/ It provides the ubiquitous features and supplies the simplest
/ definition for chooseMove
/ Author: Drew Davis
*/
package main
import "math/rand"
/* Player is the superclass of all possible players
/ and is under the AnyPlayers interface
*/
type Player struct {
name string // a string holding the name of the player
playerNum int // holds player number (1 or 2)
timeLim float64 // maximum time a player has per turn
randSeed int
}
/* newPlayer serves as the Player constructor
/ it sets name, playerNum, and timeLim as specified by its call
/ it also generates a random seed for chooseMove
*/
func newPlayer(aName string, aPlayerNum int, maxTime float64, seed int) Player {
p := Player{
name: aName,
playerNum: aPlayerNum,
timeLim: maxTime,
}
rand.Seed(int64(seed))
return p
}
/* MoveResult struct acts a tuple, holding a move and its evaluation
*/
type MoveResult struct {
move int
val int
}
/* newMoveResult serves as the MoveResult constructor
/ it sets move and val as specified by its call
*/
func newMoveResult(aMove int, aVal int) MoveResult {
m := MoveResult{
move: aMove,
val: aVal,
}
return m
}
/* Choose a move and return the int of thee column to drop ins
/ the move is randomly chosen (using provided seed in constructor)
/ this method is overidden by more advanced subclasses
/ Input: a board
/ Output: int representing which column to play in
*/
func (p Player) chooseMove(b Board) int {
// randomly select a move
aMove := -1
for (!b.legalMove(aMove)) {
aMove = rand.Intn(7) // randomly choose a move between 0 and 6
}
return aMove
}