Capture the Flag

No preview image

1 collaborator

Default-person Paul Franz (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0RC9 • Viewed 290 times • Downloaded 12 times • Run 0 times
Download the 'Capture the Flag' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;; Capture the Flag, by Paul Franz
;; EDUC 390X

;; Development History
;; Version 0.1 - January 27, 2012
;; - Creates flags, players, and playing surface
;; - Supports two players on a single computer
;; - Checks for winner, and ends "go" if condition met
;; - Moves players to edge of screen if captured; returns flag
;; - Change player color with flag capture

;; Version 0.2a - January 30, 2012
;; - Added hubnet base code
;; - Created player movement and monitoring code

;; Version 0.2b - January 31, 2012
;; - Sorted players into teams.
;; - Made players 1.5 size, and all the same shape (labeled for differentiation).
;; - Removed single-player movement code and repurposed capturing and flag-grabbing code.
;; - Made gameworld persistent after game end, so that players don't have to log back in.

;; Version 0.3 - February 1, 2012
;; - Made flags 1.5 size.
;; - Randomized grass color and adjusted neutral zone color.
;; - Added capture and rescue protocols.

;; Version 0.4 - February 1, 2012
;; - Fixed "located at:" display for players.
;; - Cleaned up "go" and "setup" codes by moving code to subroutines.
;; - Allowed for reorganizing teams when players leave.

;; Version 0.5 - February 1, 2012
;; - Added scorekeeping and option to set winning score.
;; - Reset players and flags after each score.
;; - Rebalanced teams after each score in case of quitters.
;; - Improved hubnet client so players can see playing field (theoretically) and current score.

breed [ players player ]
breed [ redflags redflag ]
breed [ blueflags blueflag ]
players-own [ user-id ]

globals [ winner team-counter team-balance blue-score red-score score ]

to startup
  hubnet-reset
  set team-counter 1
end 

to setup
  zero-variables
  reset-ticks
  reset-players
  build-field
  set-flags
  balance-teams
end 

to zero-variables
  set winner 0
  set score 0
  set blue-score 0
  set red-score 0
end 

to reset-players    ;; Resets players to starting position and conditions
  ask turtles with [color = orange ]
    [ set color red ]
  ask turtles with [color = violet ]
    [ set color blue ]
  ask turtles with [shape = "face sad"]
    [ set shape "person"]
  ask players with [color = red ]
    [ setxy (max-pxcor - 2) random-pycor ]
  ask players with [color = blue ]
    [ setxy (min-pxcor + 2) random-pycor ]
end 

to build-field  ;; Builds the fields
  ask patches [ set pcolor ((random 3) + 54) ] ;; grassifies the field
  ask patches with [pxcor = 0] ;; creates central "neutral zone"
    [ set pcolor gray + 1]
  ask patches with [pycor < min-pycor + 2 and pxcor > 7 and pxcor < 12]  ;; Creates prison areas
    [ set pcolor gray - 1]
  ask patches with [pycor < min-pycor + 2 and pxcor < -7 and pxcor > -12]
    [ set pcolor gray - 1]
end 

to set-flags  ;; (re)places flags
  ask redflags [die]
  ask blueflags [die]
  set-default-shape redflags "flag"
  set-default-shape blueflags "flag"
  create-redflags 1
    [ setxy (max-pxcor - 1) 0 
      set color red
      set size 1.5]
  create-blueflags 1
    [ setxy (min-pxcor + 1) 0
      set color blue
      set size 1.5]
end 

to balance-teams  ;; Ensures that both teams have the same number of players
  set team-balance (count players with [color = red] - count players with [color = blue])
    if team-balance > 1 [
      ask one-of players with [color = red] [set color blue
      reset-players ]]
    if team-balance < -1 [
      ask one-of players with [color = blue] [set color red
      reset-players ]]
end 

to listen-clients
  while [ hubnet-message-waiting? ]
  [
    hubnet-fetch-message
    ifelse hubnet-enter-message?
    [ create-new-player ]
    [
      ifelse hubnet-exit-message?
      [ remove-player ]
      [ ask players with [user-id = hubnet-message-source]
        [execute-command hubnet-message-tag ]
      ]]]
end 

to execute-command [command]
  if command = "Up"
  [ set heading 0
    fd 1
    stop ]
  if command = "Right"
  [ set heading 90
    fd 1
    stop ]
  if command = "Down"
  [ set heading 180
    fd 1
    stop ]
  if command = "Left"
  [ set heading 270
    fd 1
    stop ]
end 

to create-new-player
  create-players 1
  [ set user-id hubnet-message-source
    set label user-id
    assign-players
    hubnet-send user-id "Located at:" (word "(" pxcor "," pycor ")")
    hubnet-send user-id "Red" (red-score)
    hubnet-send user-id "Blue" (blue-score)
  ]
end 

to assign-players  ;; distributes new players to teams
  if team-counter = 1
    [ set color red
      set shape "person"
      setxy (max-pxcor - 2) random-pycor ]
  if team-counter = 0
    [ set color blue
      set shape "person" 
      setxy (min-pxcor + 2) random-pycor ]
  set size 1.5
  ifelse team-counter = 1 [set team-counter 0] [set team-counter 1 ]
end   

to remove-player
  ask players with [ user-id = hubnet-message-source ] [die]
end 

to go
  listen-clients
  every 0.1 [ tick ]
  if winner = 1 [
    stop ]
  return-flag
  ask players with [color = red] [
    check-capture-blue
    grab-flag-red
    liberate-red ]
  ask players with [color = blue] [
    check-capture-red
    grab-flag-blue
    liberate-blue ]
  ask players [
    check-victory
    hubnet-send user-id "Located at:" (word "(" pxcor "," pycor ")")
    hubnet-send user-id "Red" (red-score)
    hubnet-send user-id "Blue" (blue-score) ]
  prison-guard
  if score = 1 [reset]
end 

to prison-guard ;; Keeps captured players in jail
  ask players with [shape = "face sad" and color = blue and pcolor != gray - 1]
    [ setxy 8 min-pycor ]
  ask players with [shape = "face sad" and color = red and pcolor != gray - 1]
    [ setxy -8 min-pycor ]
end 

to return-flag    ;; Returns flags if players carrying them are captured
  if count turtles with [color = orange] = 0 and count blueflags = 0
     [create-blueflags 1
      [ setxy (min-pxcor + 1) 0
      set color blue]]
  if count turtles with [color = violet] = 0 and count redflags = 0
     [create-redflags 1
      [ setxy (max-pxcor - 1) 0 
      set color red]]
end 

to check-capture-blue ;; allows red players to capture and imprison opponents
  let bcaptured one-of players-here with [color = blue]
  if bcaptured != nobody and xcor > 0
    [ ask bcaptured [setxy 8 min-pycor]
      ask bcaptured [set color blue]
      ask bcaptured [set shape "face sad"] ]
end 

to check-capture-red  ;; allows blue players to capture and imprison opponents
  let rcaptured one-of players-here with [color = red]
  if rcaptured != nobody and xcor < 0
    [ ask rcaptured [setxy max-pxcor max-pycor]
      ask rcaptured [set color red]
      ask rcaptured [set shape "face sad"] ]
end   

to grab-flag-red  ;; allows players to capture the flag
  let flagcaptured one-of blueflags-here
  if flagcaptured != nobody
    [ ask flagcaptured [die]
      set color orange]   ;; changes player color to reflect captured flag
end 

to grab-flag-blue  ;; allows blue players to capture the red flag
  let flagcaptured one-of redflags-here
  if flagcaptured != nobody
    [ ask flagcaptured [die]
      set color violet]  ;; changes player color to reflect captured flag
end 

to liberate-red  ;; allows red players to rescue captured teammates
  if shape != "face sad"
  [ let rliberated one-of players-here with [color = red and pcolor = gray - 1]
  if rliberated != nobody
    [ ask rliberated [set shape "person"]]]
end 

to liberate-blue ;; allows blue players to rescue captured teammates
  if shape != "face sad"
  [ let bliberated one-of players-here with [color = blue and pcolor = gray - 1]
  if bliberated != nobody
    [ ask bliberated [set shape "person"]]]
end 

to check-victory  ;; Adds a point to a team when it captures a flag
  if color = violet and xcor = -1
    [ set blue-score blue-score + 1 
      set score 1 ]
  if color = orange and xcor = 1
    [ set red-score red-score + 1 
      set score 1 ]
  if blue-score = winning-score [blue-victory]  ;; checks for victory
  if red-score = winning-score [red-victory]
end 

to reset
   reset-players
   set-flags
   balance-teams
   set score 0
end 

to blue-victory
  print "Blue Wins"
  set winner 1
end 

to red-victory
  print "Red Wins"
  set winner 1
end 

There is only one version of this model, created over 7 years ago by Paul Franz.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.