Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Chess
- function initField()
- field={}
- for x=1,8 do
- field[x]={}
- for y=1,8 do
- field[x][y]={}
- field[x][y].type="free"
- end
- end
- end
- function cultivateField()
- for i=1,8 do
- addPawn(i,2,"black")
- addPawn(i,7,"white")
- end
- addTower(1,1,"black")
- addKnight(1,2,"black")
- addBishop(1,3,"back")
- addQueen(1,4,"black")
- addKing(1,5,"black")
- addBishop(1,6,"black")
- addKnight(1,7,"back")
- addTower(1,8,"black")
- addTower(1,1,"white")
- addKnight(1,2,"white")
- addBishop(1,3,"back")
- addQueen(1,4,"white")
- addKing(1,5,"white")
- addBishop(1,6,"white")
- addKnight(1,7,"back")
- addTower(1,8,"white")
- end
- function fieldValid(x,y)
- if x>0 and y>0 and x<=8 and y<=8 then
- return true
- else
- return false
- end
- end
- function playerOnField(x,y)
- if field[x][y].type~="free" then
- return field[x][y].color
- else
- return nil
- end
- end
- function addPawn(x,y,color)
- pawn={}
- pawn.x=x
- pawn.y=y
- pawn.color=color
- movesTaken=0
- function pawn.getLegalMoves()
- possibleMoves={}
- if color=="white" then
- --check for killoptions
- if fieldValid(x-1,y-1) then
- if playerOnField(x-1,y-1) then
- possibleMoves[#possibleMoves+1]={x=x-1,y=y-1}
- end
- end
- if fieldValid(x+1,y-1) then
- if playerOnField(x+1,y-1) then
- possibleMoves[#possibleMoves+1]={x=x+1,y=y-1}
- end
- end
- --check if first move
- if movesTaken==0 then
- if fieldValid(x,y-2) then
- if playerOnField(x,y-2)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y-2}
- end
- end
- end
- --check front
- if fieldValid(x,y-1) then
- if playerOnField(x,y-1)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y-1}
- end
- end
- else if color=="black" then
- --check for killoptions
- if fieldValid(x-1,y+1) then
- if playerOnField(x-1,y+1) then
- possibleMoves[#possibleMoves+1]={x=x-1,y=y+1}
- end
- end
- if fieldValid(x+1,y+1) then
- if playerOnField(x+1,y+1) then
- possibleMoves[#possibleMoves+1]={x=x+1,y=y-1}
- end
- end
- --check if first move
- if movesTaken==0 then
- if fieldValid(x,y+2) then
- if playerOnField(x,y+2)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y+2}
- end
- end
- end
- --check front
- if fieldValid(x,y+1) then
- if playerOnField(x,y+1)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y+1}
- end
- end
- end
- end
- pawn.possibleMoves=possibleMoves
- field[x][y]=pawn
- return pawn
- end
- function addTower(x,y,color)
- tower={}
- tower.x=x
- tower.y=y
- tower.color=color
- movesTaken=0
- function tower.getLegalMoves()
- possibleMoves={}
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x+i,y) then
- if playerOnField(x+i,y)==nil then
- possibleMoves[#possibleMoves+1]={x=x+i,y=y}
- else
- if playerOnField(x+i,y)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x+i,y=y}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x-i,y) then
- if playerOnField(x-i,y)==nil then
- possibleMoves[#possibleMoves+1]={x=x-i,y=y}
- else
- if playerOnField(x-i,y)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x-i,y=y}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x,y-i) then
- if playerOnField(x,y-i)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y-i}
- else
- if playerOnField(x,y-i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x,y=y-i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x,y+i) then
- if playerOnField(x,y+i)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y+i}
- else
- if playerOnField(x,y+i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x,y=y+i}
- noObstacle=false
- end
- end
- end
- end
- end
- tower.possibleMoves=possibleMoves
- field[x][y]=tower
- return tower
- end
- function addBishop(x,y,color)
- bishop={}
- bishop.x=x
- bishop.y=y
- bishop.color=color
- movesTaken=0
- function bishop.getLegalMoves()
- possibleMoves={}
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x+i,y+i) then
- if playerOnField(x+i,y+i)==nil then
- possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
- else
- if playerOnField(x+i,y+i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x-i,y+i) then
- if playerOnField(x-i,y+i)==nil then
- possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
- else
- if playerOnField(x-i,y+i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x+i,y-i) then
- if playerOnField(x+i,y-i)==nil then
- possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
- else
- if playerOnField(x+i,y-i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x-i,y-i) then
- if playerOnField(x-i,y-i)==nil then
- possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
- else
- if playerOnField(x-i,y-i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
- noObstacle=false
- end
- end
- end
- end
- end
- bishop.possibleMoves=possibleMoves
- field[x][y]=bishop
- return bishop
- end
- function addQueen(x,y,color)
- queen={}
- queen.x=x
- queen.y=y
- queen.color=color
- movesTaken=0
- function queen.getLegalMoves()
- possibleMoves={}
- noObstacle=true
- while noObstacle do
- if fieldValid(x+i,y) then
- if playerOnField(x+i,y)==nil then
- possibleMoves[#possibleMoves+1]={x=x+i,y=y}
- else
- if playerOnField(x+i,y)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x+i,y=y}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x-i,y) then
- if playerOnField(x-i,y)==nil then
- possibleMoves[#possibleMoves+1]={x=x-i,y=y}
- else
- if playerOnField(x-i,y)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x-i,y=y}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x,y-i) then
- if playerOnField(x,y-i)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y-i}
- else
- if playerOnField(x,y-i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x,y=y-i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x,y+i) then
- if playerOnField(x,y+i)==nil then
- possibleMoves[#possibleMoves+1]={x=x,y=y+i}
- else
- if playerOnField(x,y+i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x,y=y+i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x+i,y+i) then
- if playerOnField(x+i,y+i)==nil then
- possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
- else
- if playerOnField(x+i,y+i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x-i,y+i) then
- if playerOnField(x-i,y+i)==nil then
- possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
- else
- if playerOnField(x-i,y+i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x+i,y-i) then
- if playerOnField(x+i,y-i)==nil then
- possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
- else
- if playerOnField(x+i,y-i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
- noObstacle=false
- end
- end
- end
- end
- i=1
- noObstacle=true
- while noObstacle do
- if fieldValid(x-i,y-i) then
- if playerOnField(x-i,y-i)==nil then
- possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
- else
- if playerOnField(x-i,y-i)==color then
- noObstacle=false
- else
- possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
- noObstacle=false
- end
- end
- end
- end
- end
- queen.possibleMoves=possibleMoves
- field[x][y]=queen
- return queen
- end
- function addKing(x,y,color)
- king={}
- king.x=x
- king.y=y
- king.color=color
- movesTaken=0
- function king.getLegalMoves()
- possibleMoves={}
- for x1=-1,1 do
- for y1=-1,1 do
- if x1~=0 and y1~=0 then
- if fieldValid(x+x1,y+y1) then
- if playerOnField(x+x1,y+y1)~=color then
- possibleMoves[#possibleMoves+1]={x=x+x1,y=y+y1}
- end
- end
- end
- end
- end
- end
- king.possibleMoves=possibleMoves
- field[x][y]=king
- return king
- end
- function addKnight(x,y,color)
- knight={}
- knight.x=x
- knight.y=y
- knight.color=color
- movesTaken=0
- function knight.getLegalMoves()
- possibleMoves={}
- relativCoords={{x=2,y=1},{x=2,y=-1},{x=1,y=2},{x=1,y=-2},{x=-1,y=2},{x=-1,y=2},{x=-2,y=1},{x=-2,y=-1}}
- for i=1,#relativCoords do
- if fieldValid(x+x1,y+y1) then
- if playerOnField(x+x1,y+y1)~=color then
- possibleMoves[#possibleMoves+1]={x=x+x1,y=y+y1}
- end
- end
- end
- end
- knight.possibleMoves=possibleMoves
- field[x][y]=knight
- return knight
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement