Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- repeat
- print("Which player are you? (1 or 2)")
- compplayer = read()
- compplayer = compplayer+1-1
- if compplayer == 1 then otherplayer = 2 end
- if compplayer == 2 then otherplayer = 1 end
- until compplayer == 1 or compplayer == 2
- player = 2
- onarow = 1
- fiar = false
- nmds = 0
- nmdg = 0
- lmc = 0
- lmr = 0
- field = {}
- for i=1,7 do
- field[i] = {}
- for j=1,6 do
- field[i][j] = 0
- end
- end
- for s=1, 6 do
- if s == 1 then sidecheck = fs.exists("disk/connectmp")
- else sidecheck = fs.exists("disk"..s.."/connectmp")
- end
- if sidecheck == true
- then
- if s == 1 then path = "disk/"
- else
- path = ("disk"..s.."/")
- end
- end
- end
- file = fs.open(path.."moveplayerone", "w")
- file:close()
- file = fs.open(path.."moveplayertwo", "w")
- file:close()
- function printfield()
- term.clear()
- term.setCursorPos(1,1)
- for i=1,6 do
- for j=1,7 do
- write(field[j][i].." ")
- end
- print()
- end
- end
- function hasemptyspot(column)
- check = 1
- for n=1, 6 do
- check = check * field[column][n]
- end
- if check == 0
- then
- return true
- end
- end
- function setpiece(column)
- n = 6
- column = column + 1 - 1
- while true do
- if field[column][n] == 0 then break end
- n=n-1
- end
- field[column][n] = player
- lmc = column
- lmr = n
- end
- function changeplayer()
- if player == 2
- then player = 1
- else
- if player == 1 then player = 2 end
- end
- end
- function cfwhelp(n,i,j)
- lmch = lmc + n*i
- lmrh = lmr + n*j
- if i ~= 0 or j ~= 0 then
- if lmch >= 1 and lmch <= 7 and lmrh >= 1 and lmrh <= 6
- then
- if field[lmch][lmrh] == player
- then
- onarow = onarow + 1
- inrow = true
- if onarow >= 4 then fiar = true end
- end
- end end
- end
- function checkforwin()
- for i=-1, 1 do
- for j=-1, 1 do
- onarow = 1
- for n=1, 3 do
- inrow = false
- cfwhelp(n,i,j)
- if inrow == false then break end
- end
- for n=-1, -3, -1 do
- inrow = false
- cfwhelp(n,i,j)
- if inrow == false then break end
- end
- end
- end
- if fiar
- then
- return true
- end
- end
- function sendmove(column)
- if compplayer == 1
- then
- file = io.open(path.."moveplayerone", "w")
- file:write(column)
- file:close()
- end
- if compplayer == 2
- then
- file = io.open(path.."moveplayertwo", "w")
- file:write(column)
- file:close()
- end
- redstone.setOutput("back", true)
- sleep(0.15)
- redstone.setOutput("back", false)
- end
- function getmove()
- print()
- print()
- print("Waiting for other player. Press Q to exit if he takes too long.")
- while true do
- event,argument = os.pullEvent()
- if event == "char" and argument == "q" then os.reboot() end
- if event == "redstone" and redstone.getInput("back")
- then
- if otherplayer == 1
- then
- file = io.open(path.."moveplayerone", "r")
- column = file:read()
- file:close()
- break
- end
- if otherplayer == 2
- then
- file = io.open(path.."moveplayertwo", "r")
- column = file:read()
- file:close()
- break
- end
- end
- end
- end
- function doturn()
- pieceset = false
- repeat
- print("Turn: "..turn..". Player "..player.."'s turn. Choose a column.")
- column = read()
- if column == "quit" then os.shutdown() end
- if column ~= "1" and column ~= "2" and column ~= "3" and column ~= "4" and column ~= "5" and column ~= "6" and column ~= "7"
- then
- print("Invalid column.")
- else
- column = column + 1 - 1
- if hasemptyspot(column)
- then
- setpiece(column)
- pieceset = true
- else
- print("Column is already full.")
- end
- end
- until pieceset
- end
- turn = 0
- printfield()
- repeat
- changeplayer()
- if player == compplayer
- then
- turn = turn+1
- doturn()
- sendmove(column)
- printfield()
- print("You've set a new piece in column "..lmc..".")
- end
- if player == otherplayer
- then
- getmove()
- setpiece(column)
- printfield()
- print("Player "..player.." has set a new piece in column "..lmc..".")
- end
- until checkforwin()
- print("Player "..player.." has a four in a row and wins!")
- fs.delete( (path.."moveplayerone") )
- fs.delete( (path.."moveplayertwo") )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement