Omok (Connect 5)

Omok (Connect 5) preview image

1 collaborator

Default-person Mighty Chen (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.1.0 • Viewed 263 times • Downloaded 14 times • Run 0 times
Download the 'Omok (Connect 5)' 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

globals [current-color last-piece]

to setup
  clear-all
  ask patches [
    set pcolor white
  ]
  draw-grid
  set current-color red
  reset-ticks
end 

to go
  if place-tile [
    user-message (word whos-turn " wins!")
    stop
  ]
end 

to draw-grid
  create-turtles 1 [
    set size 0.75
    setxy min-pxcor min-pycor
    facexy xcor max-pycor
    set color gray
    pen-down
    repeat 15 [
      fd 15
      rt 90
      fd 1
      rt 270
    ]
    bk 1
    rt 90
    repeat 15 [
      fd 15
      rt 90
      fd 1
      rt 270
    ]
    die
  ]
end 

to-report place-tile
  let will-end false
  if (mouse-inside? and
    mouse-down? and
    [count turtles-here] of patch round mouse-xcor round mouse-ycor < 1 ) [
    create-turtles 1 [
      setxy round mouse-xcor round mouse-ycor
      set shape "circle"
      set size 0.75
      set color current-color
      set last-piece who

      if player-win? current-color [set will-end true]
    ]
    if will-end [report will-end]
    ifelse current-color = red [set current-color blue] [set current-color red]
  ]

  report will-end
end 

to-report player-win? [player-color]
  let num-in-row 1
  set num-in-row search-north num-in-row player-color
  set num-in-row search-south num-in-row player-color
  if num-in-row = 5 [report true]

  set num-in-row 1
  set num-in-row search-east num-in-row player-color
  set num-in-row search-west num-in-row player-color
  if num-in-row = 5 [report true]

  set num-in-row 1
  set num-in-row search-north-east num-in-row player-color
  set num-in-row search-south-west num-in-row player-color
  if num-in-row = 5 [report true]

  set num-in-row 1
  set num-in-row search-north-west num-in-row player-color
  set num-in-row search-south-east num-in-row player-color
  if num-in-row = 5 [report true]

  report false
end 

to-report search-north [num-in-row player-color]
  ask patch-at-heading-and-distance 0 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-north (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report search-south [num-in-row player-color]
  ask patch-at-heading-and-distance 180 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-south (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report search-east [num-in-row player-color]
  ask patch-at-heading-and-distance 90 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-east (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report search-west [num-in-row player-color]
  ask patch-at-heading-and-distance 270 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-west (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report search-north-east [num-in-row player-color]
  ask patch-at-heading-and-distance 45 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-north-east (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report search-north-west [num-in-row player-color]
  ask patch-at-heading-and-distance 315 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-north-west (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report search-south-east [num-in-row player-color]
  ask patch-at-heading-and-distance 135 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-south-east (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report search-south-west [num-in-row player-color]
  ask patch-at-heading-and-distance 225 1 [
    if any? turtles-here with [ color = player-color ] [
      set num-in-row search-south-west (num-in-row + 1) player-color
    ]
  ]
  report num-in-row
end 

to-report whos-turn
  ifelse current-color = red [report "Red"] [report "Blue"]
end 

to undo
  ifelse last-piece != -1 [
    ifelse current-color = red [set current-color blue] [set current-color red]
    ask turtle last-piece [ die ]
    set last-piece -1
  ] [
    user-message "Sorry, I can only undo once per round."
  ]
end 

There are 2 versions of this model.

Uploaded by When Description Download
Mighty Chen about 4 years ago undo button Download this version
Mighty Chen about 4 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Omok (Connect 5).png preview Preview for 'Omok (Connect 5)' about 4 years ago, by Mighty Chen Download

This model does not have any ancestors.

This model does not have any descendants.