Advertisement
se7enek

Untitled

Mar 27th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.07 KB | None | 0 0
  1. -- RPS - Made by brett122798
  2.  
  3. -- If you're reading this, I am sorry for my sloppy code :P
  4.  
  5.  
  6. function round(number)
  7. if number-math.floor(number) > .5 then
  8. number1 = math.ceil(number)
  9. else
  10. number1 = math.floor(number)
  11. end
  12. end
  13.  
  14. function mainmenu()
  15. term.clear()
  16. term.setCursorPos(1, 1)
  17. print(" Which type of game would you like to play? ")
  18. print("")
  19. print(" Singleplayer Multiplayer ")
  20. print(" (1) (2) ")
  21. print("-------------------------------------------------")
  22. write("Game Type: ")
  23. gametype = read()
  24. if gametype == "1" or gametype == "singleplayer" then
  25. menu()
  26. elseif gametype == "2" or gametype == "multiplayer" then
  27. hostconnect()
  28. end
  29. end
  30.  
  31. function hostconnect()
  32. term.clear()
  33. term.setCursorPos(1, 1)
  34. print(" Multiplayer ")
  35. print("-------------------------------------------------")
  36. print(" Would you like to host a game, or connect to a ")
  37. print(" game? ")
  38. print("")
  39. print(" Host Connect ")
  40. print(" (1) (2) ")
  41. print("-------------------------------------------------")
  42. write("Connection Type: ")
  43. ct = read()
  44. if ct == "1" or ct == "host" then
  45. compid = os.getComputerID()
  46. term.clear()
  47. term.setCursorPos(1, 1)
  48. print(" Multiplayer ")
  49. print("-------------------------------------------------")
  50. print(" Waiting for a player to connect.. ")
  51. print("")
  52. print("Players can connect with this ID: "..compid)
  53. print("")
  54. print("")
  55. print("If you would like to leave, terminate the program")
  56. print(" (Ctrl+T)")
  57. repeat
  58. id, message = rednet.receive()
  59. until message == "letsplay"
  60. if message == "letsplay" then
  61. opcompid = id
  62. sleep(1)
  63. rednet.send(opcompid, username)
  64. id, message = rednet.receive()
  65. opusername = message
  66. term.clear()
  67. term.setCursorPos(1, 1)
  68. print(" Multiplayer ")
  69. print("-------------------------------------------------")
  70. print(" Player Found!")
  71. print("")
  72. sleep(0.5)
  73. print(" Your opponent is "..opusername.." (ID: "..opcompid..")")
  74. sleep(3)
  75. parallel.waitForAny(multiplayermenu, info1)
  76. if message == "imdone" then
  77. multiplayermenu()
  78. end
  79. multweap = true
  80. parallel.waitForAll(multiplayerweapon, info1)
  81. multiplayergame()
  82. end
  83. elseif ct == "2" or ct == "connect" then
  84. term.clear()
  85. term.setCursorPos(1, 1)
  86. print(" Multiplayer ")
  87. print("-------------------------------------------------")
  88. print(" Type in the computer ID of the server ")
  89. print("-------------------------------------------------")
  90. write("Computer ID: ")
  91. serverconnect = read()
  92. serverconnect = tonumber(serverconnect)
  93. print("")
  94. print("Waiting for reply from server..")
  95. rednet.send(serverconnect, "letsplay")
  96. id = 0
  97. id, message = rednet.receive(5)
  98. if id == serverconnect then
  99. opcompid = id
  100. opusername = message
  101. rednet.send(opcompid, username)
  102. print("")
  103. print("Connected to the server! Please wait..")
  104. sleep(2)
  105. parallel.waitForAny(multiplayermenu, info1)
  106. if message == "imdone" then
  107. multiplayermenu()
  108. end
  109. multweap = true
  110. parallel.waitForAll(multiplayerweapon, info1)
  111. multiplayergame()
  112. else
  113. print("")
  114. print("Failed to connect to server!")
  115. sleep(2)
  116. term.clear()
  117. term.setCursorPos(1, 1)
  118. end
  119. end
  120. end
  121.  
  122. function multiplayercycle()
  123. parallel.waitForAny(multiplayermenu, info1)
  124. if message == "imdone" then
  125. multiplayermenu()
  126. end
  127. multweap = true
  128. parallel.waitForAll(multiplayerweapon, info1)
  129. multiplayergame()
  130. end
  131.  
  132. function multiplayermenu()
  133. term.clear()
  134. term.setCursorPos(1, 1)
  135. print(" *MP* Rock Paper Scissors!! V. 1.0.3 ")
  136. print("-------------------------------------------------")
  137. print(" W: "..mpwins.." | L: "..mplosses)
  138. print("-------------------------------------------------")
  139. print(" Press enter to play, but if you would like to")
  140. print(" exit the game, type 'exit' then enter.")
  141. print("")
  142. if message == "imdone" then
  143. term.setCursorPos(1, 17)
  144. print(opusername.." has chosen their weapon!")
  145. term.setCursorPos(1, 8)
  146. end
  147. write("")
  148. entergame = read()
  149. if entergame == "exit" then
  150. os.reboot()
  151. else
  152. end
  153. end
  154.  
  155. function info1()
  156. if message == "imdone" then
  157. term.setCursorPos(1, 17)
  158. print(opusername.." has chosen their weapon!")
  159. if multweap == true then
  160. term.setCursorPos(16, 6)
  161. multweap = false
  162. end
  163. else
  164. id, message = rednet.receive()
  165. term.setCursorPos(1, 17)
  166. print(opusername.." has chosen their weapon!")
  167. end
  168. end
  169.  
  170. function multiplayerweapon()
  171. term.clear()
  172. term.setCursorPos(1, 1)
  173. print(" What's your choice of Weapon? ")
  174. print("")
  175. print(" Rock Paper Scissors ")
  176. print(" (1) (2) (3) ")
  177. print("-------------------------------------------------")
  178. write("Weapon Choice: ")
  179. term.setCursorPos(16, 6)
  180. choice = read()
  181. if choice == "1" or choice == "rock" then
  182. weapon = "rock"
  183. elseif choice == "2" or choice == "paper" then
  184. weapon = "paper"
  185. elseif choice == "3" or choice == "scissors" then
  186. weapon = "scissors"
  187. else
  188. weapon = "rock"
  189. end
  190. term.setCursorPos(1, 17)
  191. rednet.send(opcompid, "imdone")
  192. if message ~= "imdone" then
  193. print("Waiting for "..opusername.."..")
  194. end
  195. end
  196.  
  197. function multiplayergame()
  198. parallel.waitForAll(oppick, pick)
  199. term.clear()
  200. term.setCursorPos(1, 1)
  201. count = 0
  202. repeat
  203. term.clear()
  204. term.setCursorPos(1, 1)
  205.  
  206. count = count + 1
  207. if weapon == "rock" then
  208. print(" You chose Rock ")
  209. elseif weapon == "paper" then
  210. print(" You chose Paper ")
  211. elseif weapon == "scissors" then
  212. print(" You chose Scissors ")
  213. end
  214. print("-------------------------------------------------")
  215. term.setCursorPos(1, 5)
  216. if count == 1 then
  217. print(" Rock.. ")
  218. elseif count == 2 then
  219. print(" Rock.. Paper.. ")
  220. elseif count == 3 then
  221. print(" Rock.. Paper.. Scissors! ")
  222. end
  223. sleep(0.6)
  224. until count == 4
  225. term.clear()
  226. term.setCursorPos(1, 1)
  227.  
  228. if weapon == opweapon then
  229. result = "tie"
  230. elseif weapon == "rock" and opweapon == "paper" then
  231. result = "lose"
  232. elseif weapon == "rock" and opweapon == "scissors" then
  233. result = "win"
  234. elseif weapon == "paper" and opweapon == "rock" then
  235. result = "win"
  236. elseif weapon == "paper" and opweapon == "scissors" then
  237. result = "lose"
  238. elseif weapon == "scissors" and opweapon == "rock" then
  239. result = "lose"
  240. elseif weapon == "scissors" and opweapon == "paper" then
  241. result = "win"
  242. end
  243. count = 5
  244. if result == "tie" then
  245. repeat
  246. term.clear()
  247. term.setCursorPos(1, 1)
  248. print(" Tie ")
  249. print("-------------------------------------------------")
  250. print(" Well, it's a tie! ")
  251. print("")
  252. print("You and "..opusername.." both got "..weapon.."!")
  253. print("")
  254. print("")
  255. print("Continuing in "..count.." seconds..")
  256. count = count - 1
  257. sleep(1)
  258. until count == 0
  259. elseif result == "win" then
  260. mpwins = mpwins + 1
  261. repeat
  262. term.clear()
  263. term.setCursorPos(1, 1)
  264. print(" Winner! ")
  265. print("-------------------------------------------------")
  266. print(" Congrats! You won the round! ")
  267. print("")
  268. print("Poor "..opusername.." got "..opweapon..".")
  269. print("")
  270. print("The Good News is that you got "..weapon.."!!")
  271. print("")
  272. print("")
  273. print("Continuing in "..count.." seconds..")
  274. count = count - 1
  275. sleep(1)
  276. until count == 0
  277. elseif result == "lose" then
  278. mplosses = mplosses + 1
  279. repeat
  280. term.clear()
  281. term.setCursorPos(1, 1)
  282. print(" Loser! ")
  283. print("-------------------------------------------------")
  284. print(" Oh no! You lost the round?! ")
  285. print("")
  286. print("Your poor choice was "..weapon..".")
  287. print("")
  288. print(opusername.."'s excellent choice was "..opweapon..".")
  289. print("")
  290. print("")
  291. print("Continuing in "..count.." seconds..")
  292. count = count - 1
  293. sleep(1)
  294. until count == 0
  295. end
  296. multiplayercycle()
  297. end
  298.  
  299. function pick()
  300. if serverconnect == false then
  301. sleep(0.01)
  302. rednet.send(opcompid, weapon)
  303. else
  304. rednet.send(opcompid, weapon)
  305. end
  306. end
  307.  
  308. function oppick()
  309. if serverconnect == true then
  310. sleep(0.01)
  311. id, message = rednet.receive()
  312. opweapon = message
  313. else
  314. id, message = rednet.receive()
  315. opweapon = message
  316. end
  317. end
  318.  
  319. function menu()
  320. term.clear()
  321. term.setCursorPos(1, 1)
  322. percentage = wins/(wins+losses)
  323. if percentage == 1 then
  324. rank = "Mr. Perfect (Lvl. 11)"
  325. elseif percentage < 1 and percentage > 0.8999999 then
  326. rank = "RPS Legend (Lvl. 10)"
  327. elseif percentage < 0.9 and percentage > 0.7999999 then
  328. rank = "RPS Master (Lvl. 9)"
  329. elseif percentage < 0.8 and percentage > 0.6999999 then
  330. rank = "Big Winner (Lvl. 8)"
  331. elseif percentage < 0.7 and percentage > 0.5999999 then
  332. rank = "Winner (Lvl. 7)"
  333. elseif percentage < 0.6 and percentage > 0.4999999 then
  334. rank = "Slight Winner (Lvl. 6)"
  335. elseif percentage < 0.5 and percentage > 0.3999999 then
  336. rank = "Minor Loser (Lvl. 5)"
  337. elseif percentage < 0.4 and percentage > 0.2999999 then
  338. rank = "Loser (Lvl. 4)"
  339. elseif percentage < 0.3 and percentage > 0.1999999 then
  340. rank = "Huge Loser (Lvl. 3)"
  341. elseif percentage < 0.2 and percentage > 0.0999999 then
  342. rank = "Computer Puncher (Lvl. 2)"
  343. elseif percentage < 0.1 and percentage > 0 then
  344. rank = "Computer Smasher (Lvl. 1)"
  345. elseif percentage == 0 then
  346. rank = "Mr. Nonperfect (Lvl. 0)"
  347. else
  348. rank = "No Rank"
  349. end
  350. round(percentage*100)
  351. percentage = number1
  352. if wins == 0 and losses == 0 then
  353. print(" ---% Rock Paper Scissors!! V. 1.0.3 ")
  354. elseif percentage < 100 and percentage > 9.9999999 then
  355. print(" "..percentage.."% Rock Paper Scissors!! V. 1.0.3 ")
  356. elseif percentage < 10 then
  357. print(" "..percentage.."% Rock Paper Scissors!! V. 1.0.3 ")
  358. elseif percentage == 100 then
  359. print(" 100% Rock Paper Scissors!! V. 1.0.3 ")
  360. end
  361. print("-------------------------------------------------")
  362. print("Rank: "..rank)
  363. term.setCursorPos(33, 3)
  364. print("W: "..wins.." | L: "..losses)
  365. print("-------------------------------------------------")
  366. print(" Press enter to play, but if you would like to")
  367. print(" exit the game, type 'exit' then enter.")
  368. print("")
  369. write("")
  370. entergame = read()
  371. if entergame == "exit" then
  372. term.clear()
  373. term.setCursorPos(1, 1)
  374. else
  375. game()
  376. end
  377. end
  378.  
  379. function game()
  380. term.clear()
  381. term.setCursorPos(1, 1)
  382. print(" What's your choice of Weapon? ")
  383. print("")
  384. print(" Rock Paper Scissors ")
  385. print(" (1) (2) (3) ")
  386. print("-------------------------------------------------")
  387. write("Weapon Choice: ")
  388. choice = read()
  389. if choice == "1" or choice == "rock" then
  390. weapon = "rock"
  391. elseif choice == "2" or choice == "paper" then
  392. weapon = "paper"
  393. elseif choice == "3" or choice == "scissors" then
  394. weapon = "scissors"
  395. else
  396. weapon = "rock"
  397. end
  398. count = 0
  399. repeat
  400. term.clear()
  401. term.setCursorPos(1, 1)
  402. count = count + 1
  403. if weapon == "rock" then
  404. print(" You chose Rock ")
  405. elseif weapon == "paper" then
  406. print(" You chose Paper ")
  407. elseif weapon == "scissors" then
  408. print(" You chose Scissors ")
  409. end
  410. print("-------------------------------------------------")
  411. term.setCursorPos(1, 5)
  412. if count == 1 then
  413. print(" Rock.. ")
  414. elseif count == 2 then
  415. print(" Rock.. Paper.. ")
  416. elseif count == 3 then
  417. print(" Rock.. Paper.. Scissors! ")
  418. end
  419. sleep(0.6)
  420. until count == 4
  421. term.clear()
  422. term.setCursorPos(1, 1)
  423. randnum = math.random(1, 3)
  424. if randnum == 1 then
  425. opweapon = "rock"
  426. elseif randnum == 2 then
  427. opweapon = "paper"
  428. elseif randnum == 3 then
  429. opweapon = "scissors"
  430. end
  431. if weapon == opweapon then
  432. result = "tie"
  433. elseif weapon == "rock" and opweapon == "paper" then
  434. result = "lose"
  435. elseif weapon == "rock" and opweapon == "scissors" then
  436. result = "win"
  437. elseif weapon == "paper" and opweapon == "rock" then
  438. result = "win"
  439. elseif weapon == "paper" and opweapon == "scissors" then
  440. result = "lose"
  441. elseif weapon == "scissors" and opweapon == "rock" then
  442. result = "lose"
  443. elseif weapon == "scissors" and opweapon == "paper" then
  444. result = "win"
  445. end
  446. if result == "tie" then
  447. print(" Tie ")
  448. print("-------------------------------------------------")
  449. print(" Well, it's a tie! ")
  450. print("")
  451. print("You and the computer both got "..weapon.."!")
  452. elseif result == "win" then
  453. wins = wins + 1
  454. file = io.open("/ProgramData/RPS/Wins", "w")
  455. file:write(wins)
  456. file:close()
  457. print(" Winner! ")
  458. print("-------------------------------------------------")
  459. print(" Congrats! You won the round! ")
  460. print("")
  461. print("The poor computer got "..opweapon..".")
  462. print("")
  463. print("The Good News is that you got "..weapon.."!!")
  464. elseif result == "lose" then
  465. losses = losses + 1
  466. file = io.open("/ProgramData/RPS/Losses", "w")
  467. file:write(losses)
  468. file:close()
  469. print(" Loser! ")
  470. print("-------------------------------------------------")
  471. print(" Oh no! You lost the round?! ")
  472. print("")
  473. print("Your poor choice was "..weapon..".")
  474. print("")
  475. print("The computer's excellent choice was "..opweapon..".")
  476. end
  477. print("")
  478. print("")
  479. sleep(2)
  480. write("Press Enter To Continue..")
  481. cont = read()
  482. menu()
  483. end
  484.  
  485. if fs.exists("ProgramData/RPS") == false then
  486. fs.makeDir("ProgramData/RPS")
  487. term.clear()
  488. term.setCursorPos(1, 1)
  489. print(" Username Selection ")
  490. print("-------------------------------------------------")
  491. print(" Please type in a username that you would like ")
  492. print("")
  493. write("Username: ")
  494. username = read()
  495. file = io.open("/ProgramData/RPS/Username", "w")
  496. file:write(username)
  497. file:close()
  498. term.clear()
  499. term.setCursorPos(1, 1)
  500. print(" Modem Side Selection ")
  501. print("-------------------------------------------------")
  502. print(" What side is you wireless modem on? ")
  503. print("")
  504. print(" Top Bottom Left ")
  505. print(" (1) (2) (3) ")
  506. print("")
  507. print(" Right Front Back ")
  508. print(" (4) (5) (6) ")
  509. print("-------------------------------------------------")
  510. write("Modem Side: ")
  511. modemside = read()
  512. if modemside == "1" or modemside == "top" then
  513. file = io.open("/ProgramData/RPS/Modem_Side", "w")
  514. file:write("top")
  515. file:close()
  516. elseif modemside == "2" or modemside == "bottom" then
  517. file = io.open("/ProgramData/RPS/Modem_Side", "w")
  518. file:write("bottom")
  519. file:close()
  520. elseif modemside == "3" or modemside == "left" then
  521. file = io.open("/ProgramData/RPS/Modem_Side", "w")
  522. file:write("left")
  523. file:close()
  524. elseif modemside == "4" or modemside == "right" then
  525. file = io.open("/ProgramData/RPS/Modem_Side", "w")
  526. file:write("right")
  527. file:close()
  528. elseif modemside == "5" or modemside == "front" then
  529. file = io.open("/ProgramData/RPS/Modem_Side", "w")
  530. file:write("front")
  531. file:close()
  532. elseif modemside == "6" or modemside == "back" then
  533. file = io.open("/ProgramData/RPS/Modem_Side", "w")
  534. file:write("back")
  535. file:close()
  536. end
  537. term.clear()
  538. term.setCursorPos(1, 1)
  539. print("Creating Save Files..")
  540. sleep(1.5)
  541. file = io.open("/ProgramData/RPS/Wins", "w")
  542. file:write("0")
  543. file:close()
  544. file = io.open("/ProgramData/RPS/Losses", "w")
  545. file:write("0")
  546. file:close()
  547. end
  548.  
  549. file = fs.open("/ProgramData/RPS/Username", "r")
  550. username = file:readAll()
  551. file:close()
  552. file = fs.open("/ProgramData/RPS/Modem_Side", "r")
  553. modemside = file:readAll()
  554. file:close()
  555. rednet.open(modemside)
  556. file = fs.open("/ProgramData/RPS/Wins", "r")
  557. wins = file:readAll()
  558. file:close()
  559. file = fs.open("/ProgramData/RPS/Losses", "r")
  560. losses = file:readAll()
  561. file:close()
  562. wins = tonumber(wins)
  563. losses = tonumber(losses)
  564. mpwins = 0
  565. mplosses = 0
  566. serverconnect = false
  567.  
  568.  
  569. mainmenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement