fork download
  1. <html>
  2. <head>
  3. <title>This is Card Title</title>
  4. <script type="text/javascript">
  5. String.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
  6. String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
  7. String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
  8. function usefloor(min,max) { return Math.floor(Math.random()*(max-min+1)+min); }
  9.  
  10. //上面是人家寫的
  11. function DQ(str) {
  12. var ret="\"" + str + "\"";
  13. return ret;
  14. }
  15. var CLUB=0;
  16. var DIAMOND=1;
  17. var HEART=2;
  18. var SPADE=3;
  19. var CardDeck= new Array(52);
  20. var COL = 8;
  21. var ROW = 23;
  22. var thisWorld=null;
  23.  
  24.  
  25. function Problem_Rule(upper,lower)
  26. {
  27. if (upper==null && lower==null)
  28. return true;
  29. if (upper.Number- lower.Number==1 && upper.isRed!= lower.isRed)
  30. return true;
  31. return false;em
  32. }
  33. function SuitToStr(inn) {
  34. switch(inn)
  35. {
  36. case CLUB: return "C";
  37. case DIAMOND: return "D";
  38. case HEART: return "H";
  39. case SPADE: return "S";
  40. default: return "@";
  41. }
  42. }
  43. function SuitColor(inn) {
  44. switch(inn)
  45. {
  46. case DIAMOND: case HEART:
  47. return true;
  48. default:
  49. return false;
  50. }
  51. }
  52. function Card_GetValueFromString(inn) {
  53.  
  54. //var str= String.prototype.trim(inn);
  55. var str=inn; //@@?
  56. if (str.length==2) {
  57. str= str.substr(0,1);
  58. return parseInt( str,10);
  59. }else if (str.length==3) {
  60. str= str.substr(0,2);
  61. return parseInt(str,10);
  62. }
  63. //document.write("Not String");
  64. return 0;
  65. }
  66. function Card_GetSuitFromString(inn){
  67. //var str= String.prototype.trim(inn);
  68. var str=inn;
  69. if (str.length==2) {
  70. str=str.substr(1,1);
  71. }else if (str.length==3) {
  72. str=str.substr(2,1);
  73. }
  74. if ( str=="C" || str=="D" || str=="H" || str=="S")
  75. return str;
  76. else
  77. return "@";
  78. }
  79. function Card_Equal(thes,that) {
  80. if (thes==null && that!=null)
  81. return false;
  82. else if (that==null && thes!=null)
  83. return false;
  84. else if (that==null && thes==null)
  85. return true;
  86. if (thes.ID==that.ID)
  87. return true;
  88. return false;
  89. }
  90. function Card_isCard(inn) {
  91. //var str= inn.trim();
  92. var str=inn; //@@?
  93. var num= Card_GetValueFromString(str);
  94. if (num>=1 && num<=13) {
  95. var ch= Card_GetSuitFromString(str);
  96. if (ch!="@") {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102.  
  103. function card(inn) {
  104. this.ID= inn;
  105. this.Suit= inn%4;
  106. this.Number= parseInt(inn/4,10)+1;
  107. this.String= " "+ this.Number + SuitToStr(this.Suit);
  108. this.isRed= SuitColor(this.Suit);
  109. }
  110. function CardDeck_GetCard2(N,S) {
  111. var SS= (-1);
  112. if (S=="C")
  113. SS=CLUB;
  114. else if (S=="D")
  115. SS=DIAMOND;
  116. else if (S=="H")
  117. SS=HEART;
  118. else if (S=="S")
  119. SS=SPADE;
  120. return CardDeck[ (N-1)*4+SS ];
  121. }
  122.  
  123. function CardDeck_GetCard1(str) {
  124. var N= Card_GetValueFromString(str);
  125. var S= Card_GetSuitFromString(str);
  126. return CardDeck_GetCard2(N,S);
  127. }
  128.  
  129. function sm(a,b)
  130. {
  131. if (a==null && b!=null)
  132. return 1;
  133. else if (a!=null && b==null)
  134. return -1;
  135. else if (a==null && b==null)
  136. return 0;
  137. if (a.Number < b.Number)
  138. return -1;
  139. else if (a.Number > b.Number)
  140. return 1;
  141. else {
  142. if (a.Suit < b.Suit)
  143. return -1;
  144. else if (a.Suit > b.Suit)
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. function MyBuffer() {
  150. this.hand= Array(4);
  151. this.add = function(inn) {
  152. for (var i=0; i<4; i++)
  153. {
  154. if (this.hand[i]==null)
  155. {
  156. this.hand[i]=inn;
  157. break;
  158. }
  159. }
  160. this.hand.sort(sm);
  161. }
  162. this.count= function(inn) {
  163. for (var i=0; i<4; i++)
  164. {
  165. if (this.hand[i]==null)
  166. return (i);
  167. }
  168. return 4;
  169. }
  170. this.search= function(inn) {
  171. for (var i=0; i< this.count(); i++)
  172. {
  173. var that= this.hand[i];
  174. if (Card_Equal(that,inn))
  175. return true;
  176. }
  177. return false;
  178. }
  179. this.remove= function(inn) {
  180. for (var i=0; i< this.count(); i++)
  181. {
  182. var that= this.hand[i];
  183. if (Card_Equal(that,inn))
  184. {
  185. this.hand[i]=null;
  186. this.hand.sort(sm);
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. this.Available= function() {
  193. return (4- this.count());
  194. }
  195. this.toString= function() {
  196. var ret="[";
  197. for(var i=0; i< this.count(); i++)
  198. {
  199. ret += this.hand[i].toString();
  200. ret += " ";
  201. }
  202. ret += "]";
  203. return ret;
  204. }
  205. this.setExample= function() {
  206. this.hand[0]= CardDeck_GetCard1("5C");
  207. this.hand[1]= CardDeck_GetCard1("12S");
  208. this.hand[2]=null;
  209. this.hand[3]=null;
  210. }
  211. }
  212. function CC(str) {
  213. return CardDeck_GetCard1(str);
  214. }
  215.  
  216. function Problem() {
  217. this.NowMaxPos=Array(8);
  218. this.MB= new MyBuffer();
  219. this.arr=new Array(8);
  220. for (var v=0; v<8; v++)
  221. {
  222. this.arr[v]= new Array(25);
  223. for (var t=0; t<25; t++)
  224. {
  225. this.arr[v][t]=null;
  226. }
  227. }
  228. this.FirstBlankLine= function() {
  229. for (var i=0; i<8; i++)
  230. {
  231. if (this.NowMaxPos[i]==0)
  232. return (i+1);
  233. }
  234. return -1;
  235. }
  236. this.HowManyBlankLines= function() {
  237. var ret=0;
  238. for (var i=0; i<8; i++)
  239. {
  240. if (this.NowMaxPos[i]==0)
  241. ++ret;
  242. }
  243. return ret;
  244. }
  245. this.MaxOf_NowMaxPos= function() {
  246. var ret=0;
  247. for (var i=0; i<8; i++)
  248. {
  249. if (ret< this.NowMaxPos[i])
  250. {
  251. ret= this.NowMaxPos[i];
  252. }
  253. }
  254. return ret;
  255. }
  256. this.SecondOf_NowMaxPos= function() {
  257. var tmp=Array(8);
  258. for (var i=0; i<8; i++)
  259. {
  260. tmp[i]= this.NowMaxPos[i];
  261. }
  262. tmp.sort();
  263. return tmp[6];
  264. }
  265. this.PeekCard = function(ccol,rrow) {
  266. if (ccol>=1 && ccol<=8 && rrow>=0 && rrow< this.NowMaxPos[ccol-1]) {
  267. return arr[ccol-1][rrow];
  268. }
  269. return null;
  270. }
  271. this.Upd_NowMaxPos= function() {
  272. for (var i=0; i<8; i++)
  273. {
  274. var j=0;
  275. while ( this.arr[i][j] != null)
  276. {
  277. j++;
  278. }
  279. this.NowMaxPos[i]=j;
  280. }
  281. }
  282. this.BufferSize= function() {
  283. return this.MB.count();
  284. }
  285. this.PeekBuffer= function(inn) {
  286. return(this.MB.hand[inn]);
  287. }
  288. this.SearchBuffer=function(inn) {
  289. return( this.MB.Search(inn));
  290. }
  291. this.setNumber=function(inn) {
  292. this.MB=new MyBuffer();
  293. var i,j,col,pos;
  294. var wLeft=52;
  295. //@@? Dim Choose As Random = New Random(inn)
  296. var deck=new Array(52);
  297. for (i=0; i<52; i++)
  298. {
  299. deck[i]= CardDeck[i];
  300. }
  301. for (i=0; i<52; i++)
  302. {
  303. j=usefloor(0,100) % wLeft;
  304. this.arr[i%8][Math.floor(i/8)]= deck[j];
  305. wLeft--;
  306. deck[j]= deck[wLeft];
  307. }
  308. this.NowMaxPos[0]=7;
  309. this.NowMaxPos[1]=7;
  310. this.NowMaxPos[2]=7;
  311. this.NowMaxPos[3]=7;
  312. this.NowMaxPos[4]=6;
  313. this.NowMaxPos[5]=6;
  314. this.NowMaxPos[6]=6;
  315. this.NowMaxPos[7]=6;
  316. }
  317. this.Peek= function (col) {
  318. if (col>=1 && col<=8)
  319. {
  320. if (this.NowMaxPos[col-1]>0 )
  321. {
  322. return this.arr[ col-1][ this.NowMaxPos[col-1] -1 ];
  323. }
  324. }
  325. return null;
  326. }
  327. this.Fetch= function(col) {
  328. var ret=null;
  329. if (col>=1 && col<=8)
  330. {
  331. if (this.NowMaxPos[col-1] >0)
  332. {
  333. if (this.arr[ col-1][ this.NowMaxPos[col-1]-1] != null)
  334. ret=this.arr[ col-1][ this.NowMaxPos[col-1]-1];
  335. this.arr[ col-1][ this.NowMaxPos[col-1]-1]=null;
  336. this.NowMaxPos[col-1]--;
  337. return ret;
  338. }
  339. }
  340. return null;
  341. }
  342. this.FetchBuffer= function(thatcard) {
  343. if (thatcard!=null)
  344. {
  345. if (this.MB.search(thatcard))
  346. {
  347. this.MB.remove(thatcard);
  348. return thatcard;
  349. }
  350. }
  351. return null;
  352. }
  353. this.Put= function( thatcard, DstLine) {
  354. if (this.NowMaxPos[ DstLine-1] <=19)
  355. {
  356. if (thatcard != null && DstLine>=1 && DstLine<=8)
  357. {
  358. this.arr[DstLine-1][ this.NowMaxPos[DstLine-1] ]=thatcard;
  359. this.NowMaxPos[DstLine-1] +=1;
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. this.PutBuffer= function(thatcard) {
  366. if (this.MB.Available()>0 && this.MB.search(thatcard)==false)
  367. {
  368. this.MB.add(thatcard);
  369. return true;
  370. }
  371. return false;
  372. }
  373. this.Pop= function(Line) {
  374. var that= this.Fetch(Line);
  375. if (that!=null)
  376. {
  377. if (this.MB.Available()>=1)
  378. {
  379. this.MB.add(that);
  380. return true;
  381. }
  382. else if (this.HowManyBlankLines()>=1 )
  383. {
  384. var tmp= this.FirstBlankLine();
  385. return this.Put(that,tmp);
  386. }
  387. }
  388. return false;
  389. }
  390. this.Move= function(SrcLine, DstLine) {
  391. var hand1= this.Peek(DstLine);
  392. var hand2= this.Peek(SrcLine);
  393. if (hand1!=null && SrcLine>=1 && DstLine!=SrcLine)
  394. {
  395. if (hand2!=null)
  396. {
  397. if (Problem_rule(hand1,hand2))
  398. {
  399. hand2= this.Fetch(SrcLine);
  400. return this.Put(hand2,DstLine);
  401. }
  402. }
  403. }else if (hand1==null)
  404. {
  405. hand2= this.Fetch(SrcLine);
  406. return this.Put(hand2,DstLine);
  407. }
  408. return false;
  409. }
  410. this.Down= function(thatcard,DstLine) {
  411. var there=null;
  412. if (this.MB.search(thatcard))
  413. there= this.FetchBuffer(thatcard);
  414. else
  415. {
  416. //Fetch(thatcard);
  417. var line= this.QuickWhereIs(thatcard);
  418. if (line>=1 && line<=8)
  419. there= this.Fetch(line);
  420. }
  421. if (there==null)
  422. {
  423. return false;
  424. }else
  425. {
  426. var upper= this.Peek(DstLine);
  427. if (upper==null)
  428. {
  429. return this.Put(thatcard,DstLine);
  430. }
  431. }
  432. return false;
  433. }
  434. this.Available=function() {
  435. var ret1= this.MB.Available();
  436. var ret2= this.HowManyBlankLines();
  437. return (ret1+ret2);
  438. }
  439. this.CARDFUNCTION=function (thatcard,delta) {
  440. if (thatcard==null)
  441. return null;
  442. var sz= this.BufferSize();
  443. if (sz>0)
  444. {
  445. for (var x=0; x<sz; x++)
  446. {
  447. if (Card_Equal(this.PeekBuffer(x), thatcard))
  448. return null;
  449. }
  450. }
  451. var i,j;
  452. var found=false;
  453. for (i=0; i<8; i++)
  454. {
  455. if (this.NowMaxPos[i] >0)
  456. {
  457. for (j=0; j< this.NowMaxPos[i] ; j++)
  458. {
  459. if (CardEqual(this.arr[i][j], thatcard))
  460. {
  461. found=true;
  462. break;
  463. }
  464. }
  465. if (found)
  466. break;
  467. }
  468. }
  469. if (j+delta >=0 && j+delta<=20)
  470. return this.arr[i][j+delta];
  471. return null;
  472. }
  473. this.WhereIs= function(thatcard) {
  474. if (thatcard!=null)
  475. {
  476. var i,j;
  477. for (i=0; i<8; i++)
  478. {
  479. for (j= this.NowMaxPos[i]-1; i>=0; i--)
  480. {
  481. if (Card_Equal(thatcard, this.arr[i][j]))
  482. {
  483. return (i+1);
  484. }
  485. }
  486. }
  487. return 0;
  488. }else
  489. return -1;
  490. }
  491. this.CardNum= function() {
  492. var sum=0;
  493. for (var i=0; i<8; i++)
  494. {
  495. sum += this.NowMaxPos[i];
  496. }
  497. sum += this.MB.count();
  498. return sum;
  499. }
  500. this.POSITION1= function(thatcard) {
  501. if (thatcard!=null)
  502. {
  503. var L= this.WhereIs(thatcard);
  504. if (L== -1 || L==0)
  505. return -1;
  506. for (var i=0; i< this.NowMaxPos[L-1]; i++)
  507. {
  508. if (Card_Equal(thatcard, this.arr[L-1][i]))
  509. return i;
  510. }
  511. }
  512. return -1;
  513. }
  514. this.POSITION2= function(thatcard, srcLine) {
  515. if (thatcard != null)
  516. {
  517. for (var i=0; i< this.NowMaxPos[length-1]; i++)
  518. {
  519. if (Card_Equal(thatcard, this.arr[srcLine-1][i]))
  520. return i;
  521. }
  522. }
  523. return -1;
  524. }
  525. this.BlankLine= function() {
  526. var ret= new Array(8);
  527. var j=0;
  528. for (var i=0; i<8; i++)
  529. {
  530. if (this.NowMaxPos[i]==0)
  531. {
  532. ret[j]=(i+1);
  533. ++j;
  534. }
  535. }
  536. return ret;
  537. }
  538. this.OneCardLine= function() {
  539. var ret= new Array(8);
  540. var j=0;
  541. for (var i=0; i<8; i++)
  542. {
  543. if (this.NowMaxPos[i]==1)
  544. {
  545. ret[j]=(i+1);
  546. j++;
  547. }
  548. }
  549. return ret;
  550. }
  551. this.toString= function() {
  552. return "@@?";
  553. }
  554. this.setExample= function() {
  555.  
  556. this.MB.setExample();
  557. this.arr[0][0]=CC("13H"); this.arr[0][1]=CC("12C"); this.arr[0][2]=CC("11H");
  558. this.arr[0][3]=CC("10C"); this.arr[0][4]=CC("9H"); this.arr[0][5]=CC("8S"); this.arr[0][6]=CC("7D");
  559.  
  560. this.arr[1][0]=CC("13S");
  561.  
  562. this.arr[2][0]=CC("2H");
  563. this.arr[2][1]=CC("6D");
  564. this.arr[2][2]=CC("5D");
  565. this.arr[2][3]=CC("11D");
  566. this.arr[2][4]=CC("2C");
  567. this.arr[2][5]=CC("8C");
  568. this.arr[2][6]=CC("9D");
  569.  
  570. this.arr[3][0]=CC("8H");
  571. this.arr[3][1]=CC("13D");
  572. this.arr[3][2]=CC("5H");
  573. this.arr[3][3]=CC("4C");
  574. this.arr[3][4]=CC("3D");
  575.  
  576. this.arr[4][0]=CC("10S");
  577. this.arr[4][1]=CC("7H");
  578. this.arr[4][2]=CC("3H");
  579. this.arr[4][3]=CC("11S");
  580. this.arr[4][4]=CC("6C");
  581. this.arr[4][5]=CC("10D");
  582. this.arr[4][6]=CC("9S");
  583.  
  584. this.arr[5][0]=CC("12D");
  585. this.arr[5][1]=CC("11C");
  586. this.arr[5][2]=CC("10H");
  587. this.arr[5][3]=CC("9C");
  588. this.arr[5][4]=CC("8D");
  589. this.arr[5][5]=CC("7C");
  590.  
  591. this.arr[6][0]=CC("12H");
  592. this.arr[6][1]=CC("4H");
  593. this.arr[6][2]=CC("2D");
  594. this.arr[6][3]=CC("6S");
  595. this.arr[6][4]=CC("4S");
  596. this.arr[6][5]=CC("6H");
  597. this.arr[6][6]=CC("5S");
  598. this.arr[6][7]=CC("4D");
  599. this.arr[6][8]=CC("3C");
  600.  
  601. this.arr[7][0]=CC("1H");
  602. this.arr[7][1]=CC("7S");
  603. this.arr[7][2]=CC("13C");
  604. this.Upd_NowMaxPos();
  605. }
  606. this.QuickWhereIs=function(thatcard) {
  607. if (this.MB.search(thatcard))
  608. return 0;
  609. for (var k=1; k<=8; k++)
  610. {
  611. var tmp =this.Peek(k)
  612. if (Card_Equal(tmp,thatcard))
  613. {
  614. return k;
  615. }
  616. }
  617. return -1;
  618. }
  619. }
  620. function Finisher(){
  621. this.C= null;
  622. this.D= null;
  623. this.H= null;
  624. this.S= null;
  625. this.Search= function(thatcard) {
  626. if (thatcard!=null)
  627. {
  628. var cmp;
  629. switch(thatcard.Suit)
  630. {
  631. case CLUB:
  632. cmp= this.C;break;
  633. case DIAMOND:
  634. cmp= this.D;break;
  635. case HEART:
  636. cmp= this.H;break;
  637. case SPADE:
  638. cmp= this.S;break;
  639. }
  640. if (cmp==null)
  641. return false;
  642. else if (cmp.Number>= thatcard.Number)
  643. return true;
  644. }
  645. return false;
  646. }
  647. this.add=function(thatcard) {
  648. if (thatcard!= null)
  649. {
  650. var top;
  651. switch(thatcard.Suit)
  652. {
  653. case CLUB:
  654. top=this.C;
  655. if (top==null || top.Number== thatcard.Number-1 )
  656. { this.C=thatcard;return true;
  657. }
  658. break;
  659. case DIAMOND:
  660. top=this.D;
  661. if (top==null || top.Number== thatcard.Number-1)
  662. { this.D=thatcard;return true;
  663. }
  664. break;
  665. case HEART:
  666. top=this.H;
  667. if (top==null || top.Number== thatcard.Number-1)
  668. { this.H= thatcard; return true;
  669. }
  670. break;
  671. case SPADE:
  672. top=this.S;
  673. if (top==null || top.Number== thatcard.Number-1)
  674. { this.S= thatcard; return true;
  675. }
  676. }
  677. }
  678. return false;
  679. }
  680. this.top= function(thatsuit) {
  681. if (thatsuit>=CLUB && thatsuit<=SPADE)
  682. {
  683. switch(thatsuit)
  684. {
  685. case CLUB:
  686. return this.C;
  687. case DIAMOND:
  688. return this.D;
  689. case HEART:
  690. return this.H;
  691. case SPADE:
  692. return this.S;
  693. }
  694. }
  695. alert("error");
  696. return null;
  697. }
  698. this.need= function(thatsuit) {
  699. if (thatsuit>=CLUB && thatsuit<=SPADE)
  700. {
  701. var top;
  702. switch(thatsuit)
  703. {
  704. case CLUB:
  705. top=this.C; break;
  706. case DIAMOND:
  707. top=this.D; break;
  708. case HEART:
  709. top= this.H; break;
  710. case SPADE:
  711. top= this.S; break;
  712. }
  713. if (top==null)
  714. { return CardDeck_GetCard2(1,thatsuit);
  715. }else if (top.Number==13)
  716. return null;
  717. else
  718. return CardDeck_GetCard2(top.Number+1, thatsuit);
  719. }
  720. alert("error");
  721. return null;
  722. }
  723. this.needed=function(thatcard) {
  724. if (thatcard!=null)
  725. {
  726. var cmpcard= this.need(thatcard.Suit);
  727. if (cmpcard==null)
  728. return false;
  729. else
  730. return (cmpcard.Number== thatcard.Number);
  731. }
  732. return false;
  733. }
  734. this.toString=function() {
  735. var ret="!";
  736. if (this.C==null)
  737. ret += "EMPTY";
  738. else
  739. ret += this.C.String;
  740.  
  741. ret += ",";
  742.  
  743. if (this.D==null)
  744. ret += "EMPTY";
  745. else
  746. ret += this.D.String;
  747.  
  748. ret += ",";
  749.  
  750. if (this.H==null)
  751. ret += "EMPTY";
  752. else
  753. ret += this.H.String;
  754.  
  755. ret += ",";
  756.  
  757. if (this.S==null)
  758. ret+= "EMPTY";
  759. else
  760. ret += this.S.String;
  761. ret += "!";
  762. return ret;
  763. }
  764. this.SafeCardUp=function(thatcard) {
  765. if (thatcard!=null)
  766. {
  767. if (thatcard.Number==1 || thatcard.Number==2)
  768. return true;
  769. if (thatcard.isRed)
  770. {
  771. if (this.S==null || this.C==null)
  772. return false;
  773. if (this.S.Number>= thatcard.Number-1 && this.C.Number>= thatcard.Number-1 )
  774. return true;
  775. return false;
  776. }else //if thatcard.isRed
  777. {
  778. if (this.H==null || this.D==null)
  779. return false;
  780. if (this.H.Number>= thatcard.Number-1 && this.D.Nubmer>= thatcard.Number-1 )
  781. return true;
  782. return false;
  783. }
  784. }
  785. return false;
  786. }
  787. this.set= function(innC,innD,innH,innS) {
  788. this.C=innC;
  789. this.D=innD;
  790. this.H=innH;
  791. this.S=innS
  792. }
  793. this.setExample= function() {
  794. this.set( CC("1C"), CC("1D"), null, CC("3S"));
  795. }
  796.  
  797. }
  798. function World(){
  799. this.P=new Problem();
  800. this.F=new Finisher();
  801. this.History=new Array(150);
  802. this.HS=0; //目前History紀錄的數目
  803. this.CONNECT= function(upper,lower) {
  804. if (Problem_Rule(upper,lower))
  805. {
  806. var str="CONNECT("+ upper.String +","+lower.String +")";
  807. //var dstLine=this.P.WhereIs(upper);
  808. var dstLine= this.P.QuickWhereIs(upper);
  809. //var srcLine=this.P.WhereIs(lower);
  810. var srcLine= this.P.QuickWhereIs(lower);
  811.  
  812. if (dstLine>=1 && dstLine<=8)
  813. {
  814. if (srcLine>=1 && srcLine<=8)
  815. {
  816. var hand= this.P.Fetch(srcLine);
  817. var ret= this.P.Put(hand,dstLine);
  818. if (ret)
  819. {
  820. this.History[this.HS]=str; this.HS++;
  821. return true;
  822. }
  823. }else if (srcLine==0)
  824. {
  825. var hand= this.P.FetchBuffer(lower);
  826. var ret= this.P.Put(hand,dstLine);
  827. if (ret)
  828. {
  829. this.History[this.HS]=str; this.HS++;
  830. return true;
  831. }
  832. }
  833. }
  834. }
  835. return false;
  836. }
  837. this.FINISH= function(thatcard) {
  838. if (thatcard!=null)
  839. {
  840. var hand=null;
  841. var str="Finish("+thatcard.String+")";
  842. var sz= this.P.BufferSize();
  843. for (var i=0; i<sz; i++)
  844. {
  845. hand= this.P.PeekBuffer(i);
  846. if (Card_Equal(thatcard,hand))
  847. {
  848. if (this.F.add(thatcard))
  849. {
  850. hand= this.P.FetchBuffer(thatcard);
  851. this.History[this.HS]=str; this.HS++;
  852. return true;
  853. }
  854. }
  855. }
  856. for (var i=1; i<=8;i++)
  857. {
  858. var hand= this.P.Peek(i);
  859. if (hand!= null)
  860. {
  861. if (Card_Equal(hand,thatcard))
  862. {
  863. if (this.F.add(thatcard))
  864. {
  865. var hand= this.P.Fetch(i);
  866. this.History[this.HS]=str; this.HS++;
  867. return true;
  868. }
  869. }
  870. }
  871. }
  872. }
  873. return false;
  874. }
  875. this.POP= function(thatcard) {
  876. if (thatcard!=null)
  877. {
  878. if (this.P.BufferSize()<4)
  879. {
  880. var str= "POP("+ thatcard.String +")";
  881. for (var i=1; i<=8; i++)
  882. {
  883. var hand= this.P.Peek(i);
  884. if (Card_Equal(hand,thatcard))
  885. {
  886. if (this.P.Pop(i))
  887. {
  888. this.History[this.HS]= str; this.HS++;
  889. return true;
  890. }
  891. }
  892. }
  893. }
  894. }
  895. return false;
  896. }
  897. this.DOWN=function(thatcard) {
  898. var dstLine= this.P.FirstBlankLine();
  899. var sz= this.P.BufferSize();
  900. for (var i=0; i<sz; i++)
  901. {
  902. var hand= this.P.PeekBuffer(i);
  903. if (Card_Equal(thatcard, hand))
  904. {
  905. if (this.P.FethBuffer(i))
  906. {
  907. if (this.P.Put(hand,dstLine))
  908. return true;
  909. }
  910. }
  911. }
  912. return false;
  913. }
  914. this.AutoSafeUp=function() {
  915. var flag=false, ret=false;
  916. do
  917. {
  918. flag=false;
  919. var hand;
  920. var sz= this.P.BufferSize();
  921. if (sz>0)
  922. {
  923. for (var i=0; i<sz; i++)
  924. {
  925. hand= this.P.PeekBuffer(i);
  926. if (hand!= null)
  927. {
  928. if (this.F.SafeCardUp(hand))
  929. {
  930. if (this.FINISH(hand))
  931. {
  932. flag=true;
  933. ret=true;
  934. }
  935. }
  936. }
  937. }
  938. }
  939. for (var i=1; i<=8; i++)
  940. {
  941. hand= this.P.Peek(i);
  942. if (hand!= null)
  943. {
  944. if (this.F.SafeCardUp(hand))
  945. {
  946. if (this.FINISH(hand))
  947. {
  948. flag=true;
  949. ret=true;
  950. }
  951. }
  952. }
  953. }
  954. } while(flag);
  955. }
  956. this.isComplete=function() {
  957. if (this.F.C.Number==13 && this.F.D.Number==13 && this.F.H.Number==13 && this.F.S.Number==13)
  958. return true;
  959. return false;
  960. }
  961. this.isDead=function() {
  962. if (this.isComplete())
  963. return false;
  964. //有點難寫
  965. return false;
  966. }
  967. this.MOVELINE=function (srcLine,dstLine) {
  968. return false;
  969. }
  970. this.HistoryToString=function() {
  971. var ret="";
  972. for (var i=0; i< this.HS-1; i++)
  973. {
  974. ret += this.History[i];
  975. ret += ",";
  976. }
  977. ret += this.History[ this.HS-1];
  978. return ret;
  979. }
  980. this.setExample= function() {
  981. //P F H HS
  982. this.F.setExample();
  983. this.P.setExample();
  984. this.History= new Array(150);
  985. this.HS=0;
  986. }
  987. }
  988.  
  989.  
  990. function init_CardDeck() {
  991. var i;
  992. for(i=0; i<52; i++) {
  993. CardDeck[i]= new card(i);
  994. }
  995. }
  996. function init_MyBuffer() {
  997. // do nothing
  998. }
  999. function init_Problem(){
  1000. // do nothing
  1001. }
  1002. function FinisherButton() {
  1003. alert("FinishButton");
  1004. var h=getHand().trim();
  1005. if (h!="EMPTY")
  1006. {
  1007. var that=CC(h);
  1008. if (thisWorld.FINISH(that))
  1009. {
  1010. setElem("HanD","EMPTY");
  1011. setElem("ErrW","");
  1012. main();
  1013. }else
  1014. {
  1015. setElem("ErrW",h+"不能放到完成區");
  1016. }
  1017. }
  1018. }
  1019. function BTN(str) {
  1020. alert("BTN "+str);
  1021. str=str.trim();
  1022. var h=getHand().trim();
  1023. if (h=="EMPTY")
  1024. {
  1025. setElem("HanD",str);
  1026. setElem("ErrW","");
  1027. }else if (h==str) {
  1028. setElem("HanD","EMPTY");
  1029. setElem("ErrW","");
  1030. }else
  1031. {
  1032. var low=CC(h);
  1033. var high=CC(str);
  1034. if (Problem_Rule(high,low))
  1035. {
  1036. if (thisWorld.CONNECT(high,low))
  1037. {
  1038. setElem("HanD","EMPTY");
  1039. setElem("ErrW","");
  1040. main();
  1041. }
  1042. }else
  1043. {
  1044. setElem("HanD","EMPTY");
  1045. setElem("ErrW","你不能連接"+high.String+"和"+low.String);
  1046. }
  1047. }
  1048. }
  1049. function Cell(str) {
  1050. alert("Cell"+ str);
  1051. str=str.trim();
  1052. var h=getHand().trim();
  1053. if (h=="EMPTY")
  1054. {
  1055. setElem("HanD",str);
  1056. setElem("ErrW","");
  1057. }else
  1058. {
  1059. setElem("HanD","EMPTY");
  1060. setElem("ErrW","");
  1061. }
  1062. }
  1063. function Empty() {
  1064. alert("Empty");
  1065. var h=getHand().trim();
  1066. if (h!="EMPTY")
  1067. {
  1068. var that=CC(h);
  1069. //var test=thisWorld.P.MB.search(that);
  1070. if (thisWorld.P.MB.search(that)==false)
  1071. {
  1072. if (thisWorld.POP(that))
  1073. {
  1074. setElem("HanD","EMPTY");
  1075. setElem("ErrW","");
  1076. main();
  1077. }
  1078. }
  1079. }
  1080. }
  1081. function ETLine(line) {
  1082. alert("ETLine: "+line);
  1083. var h=getHand().trim();
  1084. if (h!="EMPTY")
  1085. {
  1086. var that=CC(h);
  1087. if (thisWorld.P.Down(that,line))
  1088. {
  1089. setElem("HanD","EMPTY");
  1090. setElem("ErrW","");
  1091. main();
  1092. }
  1093. }
  1094. }
  1095. function init_World() {
  1096. thisWorld= new World();
  1097. thisWorld.setExample();
  1098. }
  1099. function setElem(Target,Elem){
  1100. var x=document.getElementById(Target);
  1101. x.innerHTML=Elem;
  1102. }
  1103. function addHistory(Elem) {
  1104. var x=document.getElementById('HisT');
  1105. var e= x.innerHTML;
  1106. if (e.length!= 0)
  1107. x.innerHTML= e+','+Elem;
  1108. else
  1109. x.innerHTML=Elem;
  1110. }
  1111. function getHand(){
  1112. var x=document.getElementById('HanD');
  1113. return x.innerHTML;
  1114. }
  1115. function PutFinisher() {
  1116. setElem("FFF",thisWorld.F.toString());
  1117. }
  1118. function PutFreeCell() {
  1119. //資料在 this.P.MB.Hand
  1120. var tmp=new Array(4);
  1121. for (var i=0; i<4; i++)
  1122. {
  1123. var x= document.getElementById("C"+(i+1));
  1124. if (thisWorld.P.MB.hand[i]!=null)
  1125. {
  1126. tmp[i]= thisWorld.P.MB.hand[i].String;
  1127. x.innerHTML= tmp[i];
  1128. x.onclick=Function("Cell('"+tmp[i]+"');");
  1129. }
  1130. else
  1131. {
  1132. tmp[i]="EMPTY";
  1133. x.innerHTML="EMPTY";
  1134. x.onclick=Empty;
  1135. }
  1136. }
  1137. }
  1138. function CleanTable(inn) {
  1139. for (var i=inn.rows.length-1; i>=1; i--)
  1140. {
  1141. inn.deleteRow(i);
  1142. }
  1143. }
  1144. function PutTable(){
  1145. var x=document.getElementById("MyTablE");
  1146. x.cellPadding="15";
  1147. //先把原來的table弄空,只留12345678
  1148. CleanTable(x);
  1149. var MaxCol=thisWorld.P.MaxOf_NowMaxPos();
  1150. for (var r=0; r< MaxCol; r++)
  1151. {
  1152. x.insertRow(-1);
  1153. for (var c=1; c<=8; c++){
  1154. var tmp= x.rows[r+1].insertCell(-1);
  1155. //資料在thisWorld.P.arr[r][c-1]
  1156. var that= thisWorld.P.arr[c-1][r];
  1157. if (thisWorld.P.arr[c-1][r]!=null)
  1158. {
  1159. var that1= thisWorld.P.arr[c-1][r+1];
  1160. if (that1!=null)
  1161. {
  1162. //@@? 這裡好像不能直接用fgColor 驅動
  1163. tmp.innerHTML= thisWorld.P.arr[c-1][r].String;
  1164. if (that.isRed)
  1165. tmp.bgColor= "red";
  1166. else
  1167. tmp.bgColor= "yellow";
  1168. }
  1169. else
  1170. {
  1171. var btn=document.createElement("Button");
  1172. btn.value= that.String;
  1173. //@@?想要把BTN改色
  1174. btn.onclick=Function("BTN('"+ that.String +"');");
  1175. btn.style.fontSize="20px";
  1176. tmp.appendChild(btn);
  1177. }
  1178. }else {
  1179. if (that==null && r==0)
  1180. {
  1181. var btn=document.createElement("Button");
  1182. btn.value="ET"+c;
  1183. btn.onclick=Function("ETLine('"+ c +"');");
  1184. btn.style.fontSize="20px";
  1185. tmp.appendChild(btn);
  1186. }
  1187. else
  1188. {
  1189. tmp.innerHTML= "";
  1190. }
  1191. }
  1192. }
  1193. }
  1194. }
  1195.  
  1196. function main(){
  1197. // setElem('HanD',"main");d
  1198. // setElem('ErrW',"main");
  1199. // addHistory("Test1");
  1200. // addHistory("Test2");
  1201. // addHistory("Test3");
  1202. // thisWorld.POP(CC("13S"));
  1203. PutFinisher();
  1204. PutFreeCell();
  1205. PutTable();
  1206. }
  1207.  
  1208. function RandomWorld() {
  1209. init_Page();
  1210. thisWorld= new World();
  1211. thisWorld.P.setNumber(Math.round(Math.random()*65000));
  1212. //Math.round(Math.random()*document.body.clientWidth - lay.style.posWidth)
  1213.  
  1214. main();
  1215. }
  1216. function ExampleWorld() {
  1217. init_Page();
  1218. thisWorld= new World();
  1219. thisWorld.setExample();
  1220. main();
  1221. }
  1222. function init_Page() {
  1223. var dest=document.getElementById("MainFunction");
  1224. if (dest.children.length<=3)
  1225. {
  1226. var nd=document.createElement("div");
  1227. nd.innerHTML="New DIV";
  1228. nd.style.backgroundColor = "blue";
  1229. nd.style.color = "white";
  1230.  
  1231. var Title=document.createElement("p");
  1232. Title.innerHTML="展示新接龍JavaScript版";
  1233. var Label1=document.createElement("p");
  1234. Label1.innerHTML="你手上的牌";
  1235. var Hand= document.createElement("p");
  1236. Hand.setAttribute("id","HanD");
  1237. Hand.innerHTML="EMPTY";
  1238.  
  1239. var Label2=document.createElement("p");
  1240. Label2.innerHTML="你的錯誤狀態";
  1241. var Errw= document.createElement("p");
  1242. Errw.setAttribute("id","ErrW");
  1243. Errw.innerHTML="無";
  1244.  
  1245. var Label3=document.createElement("p");
  1246. Label3.innerHTML="你的歷史狀態";
  1247. var Hist= document.createElement("p");
  1248. Hist.setAttribute("id","HisT");
  1249. Hist.innerHTML="無";
  1250.  
  1251. var Finisher=document.createElement("p");
  1252. var FB=document.createElement("button");
  1253. Finisher.innerHTML="Finisher:";
  1254. FB.innerHTML="Not Ready";
  1255. //FB.id="FFF";
  1256. FB.setAttribute("id","FFF");
  1257. FB.onclick=FinisherButton;
  1258. Finisher.appendChild(FB);
  1259.  
  1260. var FreeCell=document.createElement("p");
  1261. var FC1=document.createElement("button");
  1262. var FC2=document.createElement("button");
  1263. var FC3=document.createElement("button");
  1264. var FC4=document.createElement("button");
  1265. FreeCell.innerHTML="FreeCell:";
  1266. FC1.id="C1"; FC2.id="C2"; FC3.id="C3"; FC4.id="C4";
  1267. FC1.innerHTML= FC2.innerHTML= FC3.innerHTML= FC4.innerHTML= "Not Ready";
  1268. FreeCell.appendChild(FC1);
  1269. FreeCell.appendChild(FC2);
  1270. FreeCell.appendChild(FC3);
  1271. FreeCell.appendChild(FC4);
  1272.  
  1273. var CellTable=document.createElement("table");
  1274. CellTable.id="MyTablE";
  1275. var TB=document.createElement("TBODY");
  1276. var TR=document.createElement("tr");
  1277. TB.appendChild(TR);
  1278. var tdarr=new Array(8);
  1279. for (i=1; i<=8; i++)
  1280. {
  1281. tdarr[i-1]= document.createElement("td");
  1282. tdarr[i-1].innerHTML=""+i;
  1283. TR.appendChild(tdarr[i-1]);
  1284. }
  1285. CellTable.appendChild(TB);
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292. dest.appendChild(nd);
  1293. dest.appendChild(Title); dest.appendChild(Label1); dest.appendChild(Hand); dest.appendChild(Label2);
  1294. dest.appendChild(Errw); dest.appendChild(Label3); dest.appendChild(Hist); dest.appendChild(Finisher);
  1295. dest.appendChild(FreeCell);
  1296. dest.appendChild(CellTable);
  1297.  
  1298.  
  1299.  
  1300. }
  1301. }
  1302. init_CardDeck();
  1303. init_World();
  1304.  
  1305. </script>
  1306. </head>
  1307. <body>
  1308. <p id="MainFunction">
  1309. <button type="button" onclick="ExampleWorld()">Example_World</button>
  1310. <button type="button" onclick="RandomWorld()">Random_World</button>
  1311. <button type="button" onclick="init_Page()">Clean</button>
  1312. </p>
  1313. <!--
  1314. <p><H1>Demo the table</H1></p>
  1315. Your Hand Status is Below
  1316. <p id="HanD" >EMPTY</p>
  1317. Your Error Status is Below
  1318. <p id="ErrW" >Nothing</p>
  1319. Your History Status is Below
  1320. <p id="HisT"> </p>
  1321. <p>Finisher: <button id="FFF" type="button" onclick="FinisherButton()">Not Ready</button></p>
  1322. FreeCell
  1323. <p> <button id="C1" type="budton" >Not Ready</button>
  1324. <button id="C2" type="button" >Not Ready</button>
  1325. <button id="C3" type="button" >Not Ready</button>
  1326. <button id="C4" type="button" >Not Ready</button></p>
  1327. <table id="MyTablE" border="1" >
  1328.  
  1329. <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td></tr>
  1330.  
  1331. </table>
  1332. -->
  1333.  
  1334.  
  1335. </body>
  1336. </html>
Runtime error #stdin #stdout 0.34s 213696KB
stdin
Standard input is empty
stdout
Standard output is empty