Advertisement
Phlimy

Connect4

Jul 5th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  1. repeat
  2. print("Which player are you? (1 or 2)")
  3. compplayer = read()
  4. compplayer = compplayer+1-1
  5. if compplayer == 1 then otherplayer = 2 end
  6. if compplayer == 2 then otherplayer = 1 end
  7. until compplayer == 1 or compplayer == 2
  8.  
  9. player = 2
  10. onarow = 1
  11. fiar = false
  12. nmds = 0
  13. nmdg = 0
  14. lmc = 0
  15. lmr = 0
  16. field = {}
  17. for i=1,7 do
  18.  field[i] = {}
  19.  for j=1,6 do
  20.   field[i][j] = 0
  21.  end
  22. end
  23.  
  24. for s=1, 6 do
  25.  if s == 1 then sidecheck = fs.exists("disk/connectmp")
  26.  else sidecheck = fs.exists("disk"..s.."/connectmp")
  27.  end
  28.  if sidecheck == true
  29.  then
  30.   if s == 1 then path = "disk/"
  31.   else
  32.    path = ("disk"..s.."/")
  33.   end
  34.  end
  35. end
  36. file = fs.open(path.."moveplayerone", "w")
  37. file:close()
  38. file = fs.open(path.."moveplayertwo", "w")
  39. file:close()
  40.  
  41.  
  42. function printfield()
  43.  term.clear()
  44.  term.setCursorPos(1,1)
  45.  for i=1,6 do
  46.   for j=1,7 do
  47.    write(field[j][i].." ")
  48.   end
  49.  print()
  50.  end
  51. end
  52.  
  53. function hasemptyspot(column)
  54.  check = 1
  55.  for n=1, 6 do
  56.   check = check * field[column][n]
  57.  end
  58.  if check == 0
  59.  then
  60.   return true
  61.  end
  62. end
  63.  
  64. function setpiece(column)
  65.  n = 6
  66. column = column + 1 - 1
  67.  while true do
  68.   if field[column][n] == 0 then break end
  69.   n=n-1
  70.  end
  71.  field[column][n] = player
  72.  lmc = column
  73.  lmr = n
  74. end
  75.  
  76. function changeplayer()
  77.  if player == 2
  78.  then player = 1
  79.  else
  80.   if player == 1 then player = 2 end
  81.  end
  82. end
  83.  
  84. function cfwhelp(n,i,j)
  85.  lmch = lmc + n*i
  86.  lmrh = lmr + n*j
  87.  if i ~= 0 or j ~= 0 then
  88.  if lmch >= 1 and lmch <= 7 and lmrh >= 1 and lmrh <= 6
  89.  then
  90.   if  field[lmch][lmrh] == player
  91.   then
  92.    onarow = onarow + 1
  93.    inrow = true
  94.    if onarow >= 4 then fiar = true end
  95.   end
  96.  end end
  97. end
  98.  
  99.  
  100. function checkforwin()
  101. for i=-1, 1 do
  102.  for j=-1, 1 do
  103.   onarow = 1
  104.   for n=1, 3 do
  105.    inrow = false
  106.    cfwhelp(n,i,j)
  107.    if inrow == false then break end
  108.   end
  109.   for n=-1, -3, -1 do
  110.    inrow = false
  111.    cfwhelp(n,i,j)
  112.    if inrow == false then break end
  113.   end
  114.  end
  115. end
  116. if fiar
  117. then
  118.  return true
  119. end
  120. end
  121.  
  122.  
  123. function sendmove(column)
  124.  if compplayer == 1
  125.  then
  126.   file = io.open(path.."moveplayerone", "w")
  127.   file:write(column)
  128.   file:close()
  129.  end
  130.  if compplayer == 2
  131.  then
  132.   file = io.open(path.."moveplayertwo", "w")
  133.   file:write(column)
  134.   file:close()
  135.  end
  136.  redstone.setOutput("back", true)
  137.  sleep(0.15)
  138.  redstone.setOutput("back", false)
  139. end
  140.  
  141.  
  142. function getmove()
  143. print()
  144. print()
  145. print("Waiting for other player. Press Q to exit if he takes too long.")
  146. while true do
  147. event,argument = os.pullEvent()
  148. if event == "char" and argument == "q" then os.reboot() end
  149. if event == "redstone" and redstone.getInput("back")
  150. then
  151.  if otherplayer == 1
  152.  then
  153.   file = io.open(path.."moveplayerone", "r")
  154.   column = file:read()
  155.   file:close()
  156.   break
  157.  end
  158.  if otherplayer == 2
  159.  then
  160.   file = io.open(path.."moveplayertwo", "r")
  161.   column = file:read()
  162.   file:close()
  163.   break
  164.  end
  165. end
  166. end
  167. end
  168.  
  169.  
  170. function doturn()
  171. pieceset = false
  172. repeat
  173. print("Turn: "..turn..". Player "..player.."'s turn. Choose a column.")
  174. column = read()
  175. if column == "quit" then os.shutdown() end
  176. if column ~= "1" and column ~= "2" and column ~= "3" and column ~= "4" and column ~= "5" and column ~= "6" and column ~= "7"
  177. then
  178.  print("Invalid column.")
  179. else
  180.  column = column + 1 - 1
  181.  if hasemptyspot(column)
  182.  then
  183.   setpiece(column)
  184.   pieceset = true
  185.  else
  186.   print("Column is already full.")
  187.  end
  188. end
  189. until pieceset
  190. end
  191.  
  192. turn = 0
  193. printfield()
  194. repeat
  195.  changeplayer()
  196.  if player == compplayer
  197.  then
  198.   turn = turn+1
  199.   doturn()
  200.   sendmove(column)
  201.   printfield()
  202.   print("You've set a new piece in column "..lmc..".")
  203.  end
  204.  if player == otherplayer
  205.  then
  206.   getmove()
  207.   setpiece(column)
  208.   printfield()
  209.   print("Player "..player.." has set a new piece in column "..lmc..".")
  210.  end
  211. until checkforwin()
  212. print("Player "..player.." has a four in a row and wins!")
  213.  
  214.  
  215. fs.delete( (path.."moveplayerone") )
  216. fs.delete( (path.."moveplayertwo") )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement