Skip to content

Commit

Permalink
Merge pull request #34 from dazorni/change-game-play
Browse files Browse the repository at this point in the history
Change game play
  • Loading branch information
dazorni committed May 31, 2016
2 parents 1457d89 + 0653bab commit f02b3ef
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 228 deletions.
4 changes: 4 additions & 0 deletions app/component/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ class Game extends React.Component {
return;
}

if (this.state.fields && this.state.fields[field] && this.state.fields[field].won) {
return;
}

if (this.state.isNextFieldRandom == false && this.state.nextField != field) {
return;
}
Expand Down
57 changes: 34 additions & 23 deletions src/storage/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,28 @@ func (storage GameStorage) Turn(game *model.Game, player model.User, position in
return turn, err
}

alreadyWonField := false

for _, previousTurn := range previousTurns {
if previousTurn.Position == turn.Position {
sameFieldErr := fmt.Sprintf("Field is already played")
return turn, errors.New(sameFieldErr)
}
}

if previousTurn.WonField == true && previousTurn.Field == turn.Field {
alreadyWonField = true
}
if storage.isFieldPlayable(turn.Field, previousTurns) == false {
fieldAlreadyWonErrMessage := fmt.Sprintf("Field already won")
return turn, errors.New(fieldAlreadyWonErrMessage)
}

if alreadyWonField == false {
var previousFieldPositions [9]bool
var previousFieldPositions [9]bool

for _, previousTurn := range previousTurns {
if previousTurn.Field == turn.Field && previousTurn.Player.Id == turn.Player.Id {
previousFieldPositions[previousTurn.PositionInField] = true
}
for _, previousTurn := range previousTurns {
if previousTurn.Field == turn.Field && previousTurn.Player.Id == turn.Player.Id {
previousFieldPositions[previousTurn.PositionInField] = true
}

turn.WonField = checkForWin(previousFieldPositions, turn.PositionInField)
}

turn.WonField = checkForWin(previousFieldPositions, turn.PositionInField)

if turn.WonField == true {
var previousWonFields [9]bool

Expand All @@ -205,21 +202,17 @@ func (storage GameStorage) Turn(game *model.Game, player model.User, position in
turn.WonGame = checkForWin(previousWonFields, turn.Field)
}

previousTurnsInNextField := 0

for _, previousTurn := range previousTurns {
if turn.NextField != previousTurn.Field {
continue
}
isNextFieldPlayable := true

previousTurnsInNextField++
if turn.Field == turn.NextField && turn.WonField {
isNextFieldPlayable = false
}

if turn.Field == turn.NextField {
previousTurnsInNextField++
if isNextFieldPlayable == true {
isNextFieldPlayable = storage.isFieldPlayable(turn.NextField, previousTurns)
}

if previousTurnsInNextField == 9 {
if isNextFieldPlayable == false {
turn.RandomField = true
}

Expand All @@ -229,6 +222,24 @@ func (storage GameStorage) Turn(game *model.Game, player model.User, position in
return turn, err
}

func (storage GameStorage) isFieldPlayable(field int, previousTurns model.Turns) bool {
isFieldPlayable := true

for _, previousTurn := range previousTurns {
if field != previousTurn.Field {
continue
}

if previousTurn.WonField == false {
continue
}

isFieldPlayable = false
}

return isFieldPlayable
}

func (storage GameStorage) updateTurnCount(game *model.Game) error {
session := storage.session.Copy()
defer session.Close()
Expand Down
Loading

0 comments on commit f02b3ef

Please sign in to comment.