Advertisement
King0fGamesYami

CCOthello

Mar 17th, 2015
2,447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.65 KB | None | 0 0
  1. --Othello by KingofGamesYami
  2.  
  3. term.clear()
  4. local board = {}
  5. for i = 1, 8 do
  6.     board[ i ] = {}
  7. end
  8. board[ 0 ] = {}
  9. board[ 9 ] = {}
  10.  
  11. board[ 4 ][ 4 ] = colors.white
  12. board[ 4 ][ 5 ] = colors.black
  13. board[ 5 ][ 4 ] = colors.black
  14. board[ 5 ][ 5 ] = colors.white
  15.  
  16. local hideMoves, AI, mon
  17.  
  18. local tArgs = { ... }
  19. for k, v in pairs( tArgs ) do
  20.     if v:lower() == "hidemoves" then
  21.         hideMoves = true
  22.     elseif v:lower() == "ai" then
  23.         AI = true
  24.     elseif v:lower() == "monitor" then
  25.         for k, v in pairs( peripheral.getNames() ) do
  26.             if peripheral.getType( v ) == "monitor" and peripheral.call( v, "isColor" ) then
  27.                 --y 14
  28.                 --x 33
  29.                 peripheral.call( v, "setTextScale", 1 )
  30.                 local x, y = peripheral.call( v, "getSize" )
  31.                 peripheral.call( v, "setTextScale", math.floor( math.min( y/12, x/33 ) ) )
  32.                 term.redirect( peripheral.wrap( v ) )
  33.                 mon = true
  34.             end
  35.         end
  36.     end
  37. end
  38.  
  39. local maxx, maxy = term.getSize()
  40. xOffset = math.floor( maxx/2 ) - 4
  41. yOffset = math.floor( maxy/2 ) - 4
  42. local turn = colors.white
  43. local moves = 60
  44.  
  45. local function flip( x, y, color )
  46.     term.setCursorPos( x +xOffset, y +yOffset )
  47.     term.setBackgroundColor( colors.green )
  48.     term.setTextColor( color == colors.white and colors.black or colors.white )
  49.     term.write( "|" )
  50.     sleep( 0.1 )
  51.     term.setCursorPos( x +xOffset, y +yOffset )
  52.     term.setTextColor( color )
  53.     term.write( "|" )
  54.     term.setCursorPos( x +xOffset, y +yOffset )
  55.     sleep( 0.2 )
  56.     board[ x ][ y ] = color
  57.     term.setTextColor( color or colors.red )
  58.     term.write( "0" )
  59. end
  60.  
  61. local function checkMove( x, y, color )
  62.     if not board[ x ] or board[ x ][ y ] then
  63.         return false
  64.     end
  65.     local isValid = false
  66.     local toFlip = {}
  67.     local flip = false
  68.     for i = y + 1, 8 do
  69.         if not board[ x ][ i ] then
  70.             break
  71.         elseif board[ x ][ i ] ~= color then
  72.             flip = true
  73.         elseif flip then
  74.             isValid = true
  75.             for _i = i - 1, y, -1 do
  76.                 toFlip[ #toFlip + 1 ] = {x = x, y = _i}
  77.             end
  78.             break
  79.         else
  80.             break
  81.         end
  82.     end
  83.     flip = false
  84.     for i = y - 1, 1, -1 do
  85.         if not board[ x ][ i ] then
  86.             break
  87.         elseif board[ x ][ i ] ~= color then
  88.             flip = true
  89.         elseif flip then
  90.             isValid = true
  91.             for _i = i + 1, y do
  92.                 toFlip[ #toFlip + 1 ] = {x = x, y = _i}
  93.             end
  94.             break
  95.         else
  96.             break
  97.         end
  98.     end
  99.     flip = false
  100.     for i = x + 1, 8 do
  101.         if not board[ i ][ y ] then
  102.             break
  103.         elseif board[ i ][ y ] ~= color then
  104.             flip = true
  105.         elseif flip then
  106.             isValid = true
  107.             for _i = i - 1, x, -1 do
  108.                 toFlip[ #toFlip + 1 ] = {x = _i, y = y}
  109.             end
  110.             break
  111.         else
  112.             break
  113.         end
  114.     end
  115.     flip = false
  116.     for i = x - 1, 1, -1 do
  117.         if not board[ i ][ y ] then
  118.             break
  119.         elseif board[ i ][ y ] ~= color then
  120.             flip = true
  121.         elseif flip then
  122.             isValid = true
  123.             for _i = i + 1, x do
  124.                 toFlip[ #toFlip + 1 ] = {x = _i, y = y}
  125.             end
  126.             break
  127.         else
  128.             break
  129.         end
  130.     end
  131.     --begin diagonal checking
  132.     flip = false
  133.     local _x, _y = x, y
  134.     while _x <= 8 and _y <= 8 do
  135.         _x = _x + 1
  136.         _y = _y + 1
  137.         if not board[ _x ][ _y ] then
  138.             break
  139.         elseif board[ _x ][ _y ] ~= color then
  140.             flip = true
  141.         elseif flip then
  142.             isValid = true
  143.             while _x ~= x and _y ~= y do
  144.                 _x = _x - 1
  145.                 _y = _y - 1
  146.                 toFlip[ #toFlip + 1 ] = { x = _x, y = _y }
  147.             end
  148.             break
  149.         else
  150.             break
  151.         end
  152.     end
  153.     flip = false
  154.     local _x, _y = x, y
  155.     while _x <= 8 and _y >= 1 do
  156.         _x = _x + 1
  157.         _y = _y - 1
  158.         if not board[ _x ][ _y ] then
  159.             break
  160.         elseif board[ _x ][ _y ] ~= color then
  161.             flip = true
  162.         elseif flip then
  163.             isValid = true
  164.             while _x ~= x and _y ~= y do
  165.                 _x = _x - 1
  166.                 _y = _y + 1
  167.                 toFlip[ #toFlip + 1 ] = { x = _x, y = _y }
  168.             end
  169.             break
  170.         else
  171.             break
  172.         end
  173.     end
  174.     flip = false
  175.     local _x, _y = x, y
  176.     while _x >= 1 and _y <= 8 do
  177.         _x = _x - 1
  178.         _y = _y + 1
  179.         if not board[ _x ][ _y ] then
  180.             break
  181.         elseif board[ _x ][ _y ] ~= color then
  182.             flip = true
  183.         elseif flip then
  184.             isValid = true
  185.             while _x ~= x and _y ~= y do
  186.                 _x = _x + 1
  187.                 _y = _y - 1
  188.                 toFlip[ #toFlip + 1 ] = { x = _x, y = _y }
  189.             end
  190.             break
  191.         else
  192.             break
  193.         end
  194.     end
  195.     flip = false
  196.     local _x, _y = x, y
  197.     while _x >=1 and _y >= 1 do
  198.         _x = _x - 1
  199.         _y = _y - 1
  200.         if not board[ _x ][ _y ] then
  201.             break
  202.         elseif board[ _x ][ _y ] ~= color then
  203.             flip = true
  204.         elseif flip then
  205.             isValid = true
  206.             while _x ~= x and _y ~= y do
  207.                 _x = _x + 1
  208.                 _y = _y + 1
  209.                 toFlip[ #toFlip + 1 ] = { x = _x, y = _y }
  210.             end
  211.             break
  212.         else
  213.             break
  214.         end
  215.     end
  216.     return isValid and toFlip or false
  217. end
  218.  
  219. local function getScore()
  220.     local black, white = 0, 0
  221.     for x = 1, 8 do
  222.         for y = 1, 8 do
  223.             if board[ x ][ y ] == colors.white then
  224.                 white = white + 1
  225.             elseif board[ x ][ y ] == colors.black then
  226.                 black = black + 1
  227.             end
  228.         end
  229.     end
  230.     return black, white
  231. end
  232.  
  233. local function showValidMoves( color )
  234.     local moves = 0
  235.     term.setTextColor( colors.brown )
  236.     for x = 1, 8 do
  237.         for y = 1, 8 do
  238.             if checkMove( x, y, color ) then
  239.                 if not hideMoves then
  240.                     term.setCursorPos( x + xOffset, y + yOffset )
  241.                     term.write( "0" )
  242.                 end
  243.                 moves = moves + 1
  244.             end
  245.         end
  246.     end
  247.     return moves == 0
  248. end
  249.  
  250. local function render()
  251.     term.setBackgroundColor( colors.green )
  252.     for x = 1, 8 do
  253.         for y = 1, 8 do
  254.             term.setCursorPos( x +xOffset, y+yOffset )
  255.             term.setTextColor( board[ x ][ y ] or colors.lightGray )
  256.             term.write( "0" )
  257.         end
  258.     end
  259. end
  260.  
  261. local function showStats()
  262.     local black, white = getScore()
  263.     term.setTextColor( colors.lightBlue )
  264.     term.setBackgroundColor( colors.blue )
  265.     term.setCursorPos( xOffset - 11, yOffset + 4 )
  266.     term.write( "Black: " .. (tostring( black ):reverse() .. "0" ):match( "%d%d" ):reverse() )
  267.     term.setCursorPos( xOffset + 12, yOffset + 4 )
  268.     term.write( "White: " .. (tostring( white ):reverse() .. "0" ):match( "%d%d" ):reverse() )
  269.     term.setCursorPos( xOffset + 2, yOffset - 2 )
  270.     term.write( "Turn: " .. (turn == colors.white and "W" or "B") )
  271. end
  272.  
  273. local function drawBorder()
  274.     term.setBackgroundColor( colors.blue )
  275.     term.setTextColor( colors.lightBlue )
  276.     term.setCursorPos( xOffset, yOffset )
  277.     term.write( "\\||||||||/" )
  278.     term.setCursorPos( xOffset, yOffset + 9 )
  279.     term.write( "/||||||||\\" )
  280.     for i = 1, 8 do
  281.         term.setCursorPos( xOffset, yOffset + i )
  282.         term.write( "=" )
  283.         term.setCursorPos( xOffset + 9, yOffset + i )
  284.         term.write( "=" )
  285.     end
  286. end
  287.  
  288. local function AIMove()
  289.     local numDots, flips, move = 1, 0
  290.     term.setBackgroundColor( colors.blue )
  291.     term.setTextColor( colors.lightBlue )
  292.     for x = 1, 8 do
  293.         term.setCursorPos( xOffset - 9, yOffset + 3 )
  294.         term.write( string.rep( ".", numDots ) )
  295.         term.write( string.rep( " ", 3 - numDots ) )
  296.         numDots = (numDots + 1) % 4
  297.         for y = 1, 8 do
  298.             local results = checkMove( x, y, colors.black )
  299.             if results and #results > flips then
  300.                 move = { x = x, y = y }
  301.                 flips = #results
  302.             elseif results and #results == flips and math.random( 1, 2 ) == 1 then
  303.                 move = { x = x, y = y }
  304.             end
  305.         end
  306.         sleep(0.05)
  307.     end
  308.     term.setBackgroundColor( colors.black )
  309.     term.setCursorPos( xOffset - 9, yOffset + 3 )
  310.     term.write( "   " )
  311.     return move
  312. end
  313.  
  314. drawBorder()
  315. render()
  316. showValidMoves( colors.white )
  317. showStats()
  318.  
  319. while moves > 0 do
  320.     local event = {os.pullEvent()}
  321.     if (mon and event[ 1 ] == "monitor_touch") or event[ 1 ] == "mouse_click" then
  322.         event[ 3 ] = event[ 3 ] - xOffset
  323.         event[ 4 ] = event[ 4 ] - yOffset
  324.         if event[ 3 ] >= 1 and event[ 3 ] <= 8 and event[ 4 ] <= 8 and event[ 4 ] >= 1 then
  325.             local result = checkMove( event[ 3 ], event[ 4 ], turn )
  326.             if result then
  327.                 board[ event[ 3 ] ][ event[ 4 ] ] = turn
  328.                 term.setCursorPos( event[ 3 ] + xOffset, event[ 4 ] + yOffset )
  329.                 term.setTextColor( turn )
  330.                 term.setBackgroundColor( colors.green )
  331.                 term.write( "0" )
  332.                 for _, v in ipairs( result ) do
  333.                     if v.x == event[ 3 ] and v.y == event[ 4 ] then
  334.                         --do nothing
  335.                     else
  336.                         flip( v.x, v.y, turn )
  337.                     end
  338.                 end
  339.                 turn = turn == colors.white and colors.black or colors.white
  340.                 moves = moves - 1
  341.                 render()
  342.                 if showValidMoves( turn ) then
  343.                     turn = turn == colors.white and colors.black or colors.white
  344.                     if showValidMoves( turn ) then
  345.                         break
  346.                     end
  347.                 end
  348.             end
  349.             showStats()
  350.         end
  351.         if AI and turn == colors.black then
  352.             local result = AIMove()
  353.             os.queueEvent( "mouse_click", 1, result.x + xOffset, result.y + yOffset )
  354.         end
  355.     elseif event[ 1 ] == "char" and event[ 2 ] == "m" then
  356.         hideMoves = not hideMoves
  357.         render()
  358.         showValidMoves( turn )
  359.     end
  360. end
  361.  
  362. term.setBackgroundColor( colors.black )
  363. term.setTextColor( colors.white )
  364. term.clear()
  365. term.setCursorPos( 1, 1 )
  366. local black, white = getScore()
  367. print( "Congrats " .. (black > white and "black" or "white") .. "! You won the game!" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement