Guest User

Untitled

a guest
May 26th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.65 KB | None | 0 0
  1. let s:GameOver=1
  2. let s:CompScore=0
  3. let s:UserScore=0
  4. let s:StageMap = ""
  5. let s:CompCoin = "o"
  6. let s:UserCoin = "x"
  7. let s:NullCoin = "."
  8. let s:oldx = 0
  9. let s:oldy = 0
  10. let s:LastPass = ""
  11.  
  12. command! -nargs=* Reverse call s:InitStage(0)
  13.  
  14. function! s:InitStage(flg)
  15. let s:StageMap = ""
  16. let l:ycnt = 0
  17. while l:ycnt <= 7
  18. let l:xcnt = 0
  19. while l:xcnt <= 7
  20. if a:flg == 1
  21. if (l:xcnt == 3 && l:ycnt == 4) || (l:xcnt == 4 && l:ycnt == 3)
  22. let s:StageMap = s:StageMap . "o"
  23. elseif (l:xcnt == 3 && l:ycnt == 3) || (l:xcnt == 4 && l:ycnt == 4)
  24. let s:StageMap = s:StageMap . "x"
  25. else
  26. let s:StageMap = s:StageMap . "."
  27. endif
  28. else
  29. let s:StageMap = s:StageMap . "."
  30. endif
  31. let l:xcnt = l:xcnt + 1
  32. endwhile
  33. let l:ycnt = l:ycnt + 1
  34. endwhile
  35.  
  36. call s:MakeStage()
  37. if !a:flg
  38. nnoremap <silent> <buffer> <space> :call <SID>PlayUser(1)<cr>
  39. nnoremap <silent> <buffer> <2-leftmouse> :call <SID>PlayUser(1)<cr>
  40. nnoremap <silent> <buffer> <esc> :call <SID>PlayUser(0)<cr>
  41. nnoremap <silent> <buffer> q :close<cr>
  42. nnoremap <silent> <buffer> s :call <SID>GameStart()<cr>
  43. else
  44. let s:CompScore=2
  45. let s:UserScore=2
  46. echohl WarningMsg
  47. echo s:UserCoin . " user : thinking..."
  48. echohl None
  49. endif
  50. endfunction
  51.  
  52. function! s:MakeStage()
  53. let save_f = @f
  54. let @f = ""
  55. let @f = @f . "+----------------+\n"
  56. let l:ycnt = 0
  57. while l:ycnt <= 7
  58. let l:xcnt = 0
  59. let @f = @f . "|"
  60. while l:xcnt <= 7
  61. let @f = @f . strpart(s:StageMap,l:ycnt*8+l:xcnt,1) . " "
  62. let l:xcnt = l:xcnt + 1
  63. endwhile
  64. let @f = @f . "|\n"
  65. let l:ycnt = l:ycnt + 1
  66. endwhile
  67. let @f = @f . "+----------------+\n"
  68. let @f = @f . "s:begin , q:quit\n"
  69. if bufloaded("Reverse")
  70. let vbufnr = bufwinnr("Reverse")
  71. exe "normal \<c-w>".vbufnr."w"
  72. silent! %d _
  73. else
  74. silent execute "11split Reverse"
  75. setlocal noswapfile
  76. setlocal buftype=nowrite
  77. setlocal bufhidden=delete
  78. endif
  79. silent put! f
  80. let @f = save_f
  81. execute "set titlestring=Reverse"
  82. endfunction
  83.  
  84. function! s:SnapCoin( where, self )
  85. let l:stage_tmp = strpart(s:StageMap,0,a:where)
  86. let l:stage_tmp = l:stage_tmp . a:self
  87. let l:stage_tmp = l:stage_tmp . strpart(s:StageMap,a:where+1)
  88. let s:StageMap = l:stage_tmp
  89. call s:MakeStage()
  90. endfunction
  91.  
  92. function! s:PickCoin( where )
  93. return strpart(s:StageMap,a:where,1)
  94. endfunction
  95.  
  96. function! s:LookCoin( where, self )
  97. let l:N = 0
  98. let l:S = 0
  99. let l:W = 0
  100. let l:E = 0
  101. let l:NW = 0
  102. let l:NE = 0
  103. let l:SW = 0
  104. let l:SE = 0
  105. if strpart(s:StageMap,a:where,1) != "."
  106. return 0
  107. else
  108. if ( ( a:where - a:where % 8 ) / 8 ) > 1
  109. let l:N = s:North( a:where,a:self )
  110. endif
  111. if ( ( a:where - a:where % 8 ) / 8 ) < 6
  112. let l:S = s:South( a:where,a:self )
  113. endif
  114. if a:where % 8 > 1
  115. let l:W = s:West( a:where,a:self )
  116. endif
  117. if a:where % 8 < 6
  118. let l:E = s:East( a:where,a:self )
  119. endif
  120. if ( ( ( a:where - a:where % 8 ) / 8 ) > 1 ) && ( a:where % 8 > 1 )
  121. let l:NW = s:NorthWest( a:where,a:self )
  122. endif
  123. if ( ( ( a:where - a:where % 8 ) / 8 ) > 1 ) && ( a:where % 8 < 6 )
  124. let l:NE = s:NorthEast( a:where,a:self )
  125. endif
  126. if ( ( ( a:where - a:where % 8 )/8 ) < 6 ) && ( a:where % 8 > 1 )
  127. let l:SW = s:SouthWest( a:where,a:self )
  128. endif
  129. if ( ( ( a:where - a:where % 8 )/8 ) < 6 ) && ( a:where % 8 < 6 )
  130. let l:SE = s:SouthEast( a:where,a:self )
  131. endif
  132. "echo "nw=".l:NW."\n"
  133. "echo "ne=".l:NE."\n"
  134. "echo "sw=".l:SW."\n"
  135. "echo "se=".l:SE."\n"
  136. "echo "s=".l:S."\n"
  137. if l:N + l:S + l:W + l:E + l:NW + l:NE + l:SW + l:SE == 0
  138. return 0
  139. else
  140. call s:MakeStage()
  141. return 1
  142. endif
  143. endif
  144. endfunction
  145.  
  146. function! s:North( where,self )
  147. let l:chk = 0
  148. let l:pos = a:where - 8
  149. while l:pos >= 0
  150. if s:PickCoin(l:pos) == a:self
  151. if l:chk != 0
  152. let l:cnt = a:where
  153. while l:cnt > l:pos
  154. call s:SnapCoin(l:cnt,a:self)
  155. let l:cnt = l:cnt - 8
  156. endwhile
  157. return 1
  158. else
  159. return 0
  160. endif
  161. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  162. let l:chk = 1
  163. else
  164. return 0
  165. endif
  166. let l:pos = l:pos - 8
  167. endwhile
  168. return 0
  169. endfunction
  170.  
  171. function! s:South( where,self )
  172. let l:chk = 0
  173. let l:pos = a:where + 8
  174. while l:pos <= 63
  175. if s:PickCoin(l:pos) == a:self
  176. if l:chk != 0
  177. let l:cnt = a:where
  178. while l:cnt < l:pos
  179. call s:SnapCoin(l:cnt,a:self)
  180. let l:cnt = l:cnt + 8
  181. endwhile
  182. return 1
  183. else
  184. return 0
  185. endif
  186. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  187. let l:chk = 1
  188. else
  189. return 0
  190. endif
  191. let l:pos = l:pos + 8
  192. endwhile
  193. return 0
  194. endfunction
  195.  
  196. function! s:West( where,self )
  197. let l:chk = 0
  198. let l:pos = a:where - 1
  199. while l:pos >= (a:where - a:where % 8)
  200. if s:PickCoin(l:pos) == a:self
  201. if l:chk != 0
  202. let l:cnt = a:where
  203. while l:cnt > l:pos
  204. call s:SnapCoin(l:cnt,a:self)
  205. let l:cnt = l:cnt - 1
  206. endwhile
  207. return 1
  208. else
  209. return 0
  210. endif
  211. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  212. let l:chk = 1
  213. else
  214. return 0
  215. endif
  216. let l:pos = l:pos - 1
  217. endwhile
  218. return 0
  219. endfunction
  220.  
  221. function! s:East( where,self )
  222. let l:chk = 0
  223. let l:pos = a:where + 1
  224. while l:pos <= (a:where - a:where % 8 + 7)
  225. if s:PickCoin(l:pos) == a:self
  226. if l:chk != 0
  227. let l:cnt = a:where
  228. while l:cnt < l:pos
  229. call s:SnapCoin(l:cnt,a:self)
  230. let l:cnt = l:cnt + 1
  231. endwhile
  232. return 1
  233. else
  234. return 0
  235. endif
  236. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  237. let l:chk = 1
  238. else
  239. return 0
  240. endif
  241. let l:pos = l:pos + 1
  242. endwhile
  243. return 0
  244. endfunction
  245.  
  246. function! s:NorthWest( where,self )
  247. let l:chk = 0
  248. let l:pos = a:where - 9
  249. while l:pos >= 0
  250. if s:PickCoin(l:pos) == a:self
  251. if l:chk != 0
  252. let l:cnt = a:where
  253. while l:cnt > l:pos
  254. call s:SnapCoin(l:cnt,a:self)
  255. let l:cnt = l:cnt - 9
  256. endwhile
  257. return 1
  258. else
  259. return 0
  260. endif
  261. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  262. if (l:pos % 8) == 0 || ((l:pos - l:pos % 8) / 8) == 0
  263. return 0
  264. else
  265. let l:chk = 1
  266. let l:pos = l:pos - 9
  267. endif
  268. else
  269. return 0
  270. endif
  271. endwhile
  272. return 0
  273. endfunction
  274.  
  275. function! s:NorthEast( where,self )
  276. let l:chk = 0
  277. let l:pos = a:where - 7
  278. while l:pos >= 0
  279. if s:PickCoin(l:pos) == a:self
  280. if l:chk != 0
  281. let l:cnt = a:where
  282. while l:cnt > l:pos
  283. call s:SnapCoin(l:cnt,a:self)
  284. let l:cnt = l:cnt - 7
  285. endwhile
  286. return 1
  287. else
  288. return 0
  289. endif
  290. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  291. if l:pos % 8 == 7 || (l:pos - l:pos % 8) / 8 == 0
  292. return 0
  293. else
  294. let l:chk = 1
  295. let l:pos = l:pos - 7
  296. endif
  297. else
  298. return 0
  299. endif
  300. endwhile
  301. return 0
  302. endfunction
  303.  
  304. function! s:SouthEast( where,self )
  305. let l:chk = 0
  306. let l:pos = a:where + 7
  307. while l:pos <= 63
  308. if s:PickCoin(l:pos) == a:self
  309. if l:chk != 0
  310. let l:cnt = a:where
  311. while l:cnt < l:pos
  312. call s:SnapCoin(l:cnt,a:self)
  313. let l:cnt = l:cnt + 7
  314. endwhile
  315. return 1
  316. else
  317. return 0
  318. endif
  319. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  320. if l:pos % 8 == 0 || (l:pos - l:pos % 8) / 8 == 7
  321. return 0
  322. else
  323. let l:chk = 1
  324. let l:pos = l:pos + 7
  325. endif
  326. else
  327. return 0
  328. endif
  329. endwhile
  330. return 0
  331. endfunction
  332.  
  333. function! s:SouthWest( where,self )
  334. let l:chk = 0
  335. let l:pos = a:where + 9
  336. while l:pos <= 63
  337. if s:PickCoin(l:pos) == a:self
  338. if l:chk != 0
  339. let l:cnt = a:where
  340. while l:cnt < l:pos
  341. call s:SnapCoin(l:cnt,a:self)
  342. let l:cnt = l:cnt + 9
  343. endwhile
  344. return 1
  345. else
  346. return 0
  347. endif
  348. elseif s:NullCoin.a:self !~ s:PickCoin(l:pos)
  349. if l:pos % 8 == 0 || (l:pos - l:pos % 8) / 8 == 9
  350. return 0
  351. else
  352. let l:chk = 1
  353. let l:pos = l:pos + 9
  354. endif
  355. else
  356. return 0
  357. endif
  358. endwhile
  359. return 0
  360. endfunction
  361.  
  362. function! s:GameStart()
  363. if s:GameOver
  364. let s:GameOver = 0
  365. call s:InitStage(1)
  366. endif
  367. endfunction
  368.  
  369. function! s:GameStop()
  370. let s:GameOver = 1
  371. endfunction
  372.  
  373. function! s:PlayUser(snap)
  374. if s:GameOver
  375. return
  376. endif
  377. let s:oldx = col(".")
  378. let s:oldy = line(".")
  379. if a:snap == 1
  380. let l:pret = s:LookCoin((line(".")-2)*8+(col(".")-2)/2,s:UserCoin)
  381. redraw!
  382. if l:pret == 0
  383. echohl Error
  384. echo "Oops. You can't put on here."
  385. echohl None
  386. 1sleep
  387. redraw!
  388. echohl WarningMsg
  389. echo s:UserCoin . " user : thinking..."
  390. echohl None
  391. return
  392. else
  393. let s:LastPass = ""
  394. let s:UserScore = s:UserScore + l:pret
  395. echohl Todo
  396. echo s:UserCoin . " user: done"
  397. echohl None
  398. endif
  399. else
  400. if s:LastPass == s:CompCoin
  401. call s:GameStop()
  402. return
  403. endif
  404. let s:LastPass = s:UserCoin
  405. redraw!
  406. echohl WarningMsg
  407. echo s:UserCoin . " user: pass"
  408. echohl None
  409. endif
  410.  
  411. 1sleep
  412. redraw!
  413. echohl WarningMsg
  414. echo s:CompCoin . " comp: thinking..."
  415. echohl None
  416. 1sleep
  417. redraw!
  418. let l:pret = s:PlayComp()
  419. if l:pret == 0
  420. if s:LastPass == s:UserCoin
  421. call s:GameStop()
  422. return
  423. endif
  424. let s:LastPass = s:UserCoin
  425. echohl Error
  426. echo s:CompCoin . " comp: pass"
  427. echohl None
  428. else
  429. let s:CompScore = s:CompScore + l:pret
  430. echohl Todo
  431. echo s:CompCoin . " comp: done"
  432. echohl None
  433. endif
  434.  
  435. 1sleep
  436. redraw!
  437. echohl WarningMsg
  438. echo s:UserCoin . " user : thinking..."
  439. echohl None
  440.  
  441. execute "normal ".s:oldy."G"
  442. execute "normal ".(s:oldx-1)."l"
  443. "execute "set titlestring=Reverse\\ ".s:UserScore.":".s:CompScore
  444. endfunction
  445.  
  446. function! s:PlayComp()
  447. let l:cget=s:LookCoin(0,s:CompCoin)
  448. if l:cget > 0
  449. return l:cget
  450. endif
  451. let l:cget=s:LookCoin(7,s:CompCoin)
  452. if l:cget > 0
  453. return l:cget
  454. endif
  455. let l:cget=s:LookCoin(56,s:CompCoin)
  456. if l:cget > 0
  457. return l:cget
  458. endif
  459. let l:cget=s:LookCoin(63,s:CompCoin)
  460. if l:cget > 0
  461. return l:cget
  462. endif
  463. let l:cget=s:LookCoin(2,s:CompCoin)
  464. if l:cget > 0
  465. return l:cget
  466. endif
  467. let l:cget=s:LookCoin(5,s:CompCoin)
  468. if l:cget > 0
  469. return l:cget
  470. endif
  471. let l:cget=s:LookCoin(16,s:CompCoin)
  472. if l:cget > 0
  473. return l:cget
  474. endif
  475. let l:cget=s:LookCoin(23,s:CompCoin)
  476. if l:cget > 0
  477. return l:cget
  478. endif
  479. let l:cget=s:LookCoin(40,s:CompCoin)
  480. if l:cget > 0
  481. return l:cget
  482. endif
  483. let l:cget=s:LookCoin(47,s:CompCoin)
  484. if l:cget > 0
  485. return l:cget
  486. endif
  487. let l:cget=s:LookCoin(58,s:CompCoin)
  488. if l:cget > 0
  489. return l:cget
  490. endif
  491. let l:cget=s:LookCoin(61,s:CompCoin)
  492. if l:cget > 0
  493. return l:cget
  494. endif
  495. let l:cget=s:LookCoin(3,s:CompCoin)
  496. if l:cget > 0
  497. return l:cget
  498. endif
  499. let l:cget=s:LookCoin(4,s:CompCoin)
  500. if l:cget > 0
  501. return l:cget
  502. endif
  503. let l:cget=s:LookCoin(24,s:CompCoin)
  504. if l:cget > 0
  505. return l:cget
  506. endif
  507. let l:cget=s:LookCoin(31,s:CompCoin)
  508. if l:cget > 0
  509. return l:cget
  510. endif
  511. let l:cget=s:LookCoin(32,s:CompCoin)
  512. if l:cget > 0
  513. return l:cget
  514. endif
  515. let l:cget=s:LookCoin(39,s:CompCoin)
  516. if l:cget > 0
  517. return l:cget
  518. endif
  519. let l:cget=s:LookCoin(59,s:CompCoin)
  520. if l:cget > 0
  521. return l:cget
  522. endif
  523. let l:cget=s:LookCoin(60,s:CompCoin)
  524. if l:cget > 0
  525. return l:cget
  526. endif
  527. let l:cget=s:LookCoin(1,s:CompCoin)
  528. if l:cget > 0
  529. return l:cget
  530. endif
  531. let l:cget=s:LookCoin(6,s:CompCoin)
  532. if l:cget > 0
  533. return l:cget
  534. endif
  535. let l:cget=s:LookCoin(8,s:CompCoin)
  536. if l:cget > 0
  537. return l:cget
  538. endif
  539. let l:cget=s:LookCoin(15,s:CompCoin)
  540. if l:cget > 0
  541. return l:cget
  542. endif
  543. let l:cget=s:LookCoin(48,s:CompCoin)
  544. if l:cget > 0
  545. return l:cget
  546. endif
  547. let l:cget=s:LookCoin(55,s:CompCoin)
  548. if l:cget > 0
  549. return l:cget
  550. endif
  551. let l:cget=s:LookCoin(57,s:CompCoin)
  552. if l:cget > 0
  553. return l:cget
  554. endif
  555. let l:cget=s:LookCoin(62,s:CompCoin)
  556. if l:cget > 0
  557. return l:cget
  558. endif
  559. let l:cget=s:LookCoin(18,s:CompCoin)
  560. if l:cget > 0
  561. return l:cget
  562. endif
  563. let l:cget=s:LookCoin(21,s:CompCoin)
  564. if l:cget > 0
  565. return l:cget
  566. endif
  567. let l:cget=s:LookCoin(42,s:CompCoin)
  568. if l:cget > 0
  569. return l:cget
  570. endif
  571. let l:cget=s:LookCoin(45,s:CompCoin)
  572. if l:cget > 0
  573. return l:cget
  574. endif
  575. let l:cget=s:LookCoin(19,s:CompCoin)
  576. if l:cget > 0
  577. return l:cget
  578. endif
  579. let l:cget=s:LookCoin(20,s:CompCoin)
  580. if l:cget > 0
  581. return l:cget
  582. endif
  583. let l:cget=s:LookCoin(26,s:CompCoin)
  584. if l:cget > 0
  585. return l:cget
  586. endif
  587. let l:cget=s:LookCoin(29,s:CompCoin)
  588. if l:cget > 0
  589. return l:cget
  590. endif
  591. let l:cget=s:LookCoin(34,s:CompCoin)
  592. if l:cget > 0
  593. return l:cget
  594. endif
  595. let l:cget=s:LookCoin(37,s:CompCoin)
  596. if l:cget > 0
  597. return l:cget
  598. endif
  599. let l:cget=s:LookCoin(43,s:CompCoin)
  600. if l:cget > 0
  601. return l:cget
  602. endif
  603. let l:cget=s:LookCoin(44,s:CompCoin)
  604. if l:cget > 0
  605. return l:cget
  606. endif
  607. let l:cget=s:LookCoin(10,s:CompCoin)
  608. if l:cget > 0
  609. return l:cget
  610. endif
  611. let l:cget=s:LookCoin(13,s:CompCoin)
  612. if l:cget > 0
  613. return l:cget
  614. endif
  615. let l:cget=s:LookCoin(17,s:CompCoin)
  616. if l:cget > 0
  617. return l:cget
  618. endif
  619. let l:cget=s:LookCoin(22,s:CompCoin)
  620. if l:cget > 0
  621. return l:cget
  622. endif
  623. let l:cget=s:LookCoin(41,s:CompCoin)
  624. if l:cget > 0
  625. return l:cget
  626. endif
  627. let l:cget=s:LookCoin(46,s:CompCoin)
  628. if l:cget > 0
  629. return l:cget
  630. endif
  631. let l:cget=s:LookCoin(50,s:CompCoin)
  632. if l:cget > 0
  633. return l:cget
  634. endif
  635. let l:cget=s:LookCoin(53,s:CompCoin)
  636. if l:cget > 0
  637. return l:cget
  638. endif
  639. let l:cget=s:LookCoin(11,s:CompCoin)
  640. if l:cget > 0
  641. return l:cget
  642. endif
  643. let l:cget=s:LookCoin(12,s:CompCoin)
  644. if l:cget > 0
  645. return l:cget
  646. endif
  647. let l:cget=s:LookCoin(25,s:CompCoin)
  648. if l:cget > 0
  649. return l:cget
  650. endif
  651. let l:cget=s:LookCoin(30,s:CompCoin)
  652. if l:cget > 0
  653. return l:cget
  654. endif
  655. let l:cget=s:LookCoin(33,s:CompCoin)
  656. if l:cget > 0
  657. return l:cget
  658. endif
  659. let l:cget=s:LookCoin(38,s:CompCoin)
  660. if l:cget > 0
  661. return l:cget
  662. endif
  663. let l:cget=s:LookCoin(51,s:CompCoin)
  664. if l:cget > 0
  665. return l:cget
  666. endif
  667. let l:cget=s:LookCoin(52,s:CompCoin)
  668. if l:cget > 0
  669. return l:cget
  670. endif
  671. let l:cget=s:LookCoin(9,s:CompCoin)
  672. if l:cget > 0
  673. return l:cget
  674. endif
  675. let l:cget=s:LookCoin(14,s:CompCoin)
  676. if l:cget > 0
  677. return l:cget
  678. endif
  679. let l:cget=s:LookCoin(49,s:CompCoin)
  680. if l:cget > 0
  681. return l:cget
  682. endif
  683. let l:cget=s:LookCoin(54,s:CompCoin)
  684. if l:cget > 0
  685. return l:cget
  686. endif
  687. return 0
  688. endfunction
Add Comment
Please, Sign In to add comment