name | correct guesses | games together | ratio |
---|
name | correct guesses | games together | ratio |
---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | USING: accessors arrays assocs classes grouping hash-sets kernel lists math math.combinatorics math.order prettyprint sequences sequences.deep sorting strings vectors ; IN: texasholdem : char>string ( char -- str ) 1 swap <string> ; : 2char>string ( a b -- string ) 2array >string ; TUPLE: card rank suit ; : <card> ( cardstr -- card ) [ first ] [ second ] bi card boa ; : <cards> ( seq -- seq ) [ <card> ] map ; : worst-hand ( -- seq ) { "2C" "4D" "5D" "6C" "7H" } <cards> ; : print-card ( card -- ) [ rank>> ] [ suit>> ] bi 2char>string . ; : =-rank ( card card -- ? ) rank>> swap rank>> = ; : =-suit ( card card -- ? ) suit>> swap suit>> = ; : rank>value ( card -- a ) rank>> char>string H{ { "2" 2 } { "3" 3 } { "4" 4 } { "5" 5 } { "6" 6 } { "7" 7 } { "8" 8 } { "9" 9 } { "T" 10 } { "J" 11 } { "Q" 12 } { "K" 13 } { "A" 14 } } at ; M: card <=> [ rank>value ] bi@ <=> ; : deck ( -- deck ) "23456789TJQKA" "SCDH" [ 2char>string ] cartesian-map flatten [ <card> ] map ; : group-by-rank ( hand -- assoc ) H{ } clone swap [ dupd rank>value swap [ dup [ 1 + ] [ drop 1 ] if ] change-at ] each ; : cmp-length-rank ( x y -- <=> ) 2dup [ second ] bi@ >=< dup +eq+ = [ drop [ first ] bi@ >=< ] [ 2nip ] if ; : rank-ordered ( hand -- seq ) group-by-rank >alist [ cmp-length-rank ] sort-with ; : rank-ordered-rank ( hand -- seq ) rank-ordered [ first ] map ; : rank-ordered-num ( hand -- seq ) rank-ordered [ second ] map ; : n-of-kind? ( hand n -- ? ) <combinations> [ [ =-rank ] monotonic? ] any? ; : four-of-kind? ( hand -- ? ) 4 n-of-kind? ; : three-of-kind? ( hand -- ? ) 3 n-of-kind? ; : pair? ( hand -- ? ) 2 n-of-kind? ; : full-house? ( hand -- ? ) rank-ordered-num [ first 3 = ] [ second 2 = ] bi and ; : flush? ( hand -- ? ) [ =-suit ] monotonic? ; : straight? ( hand -- ? ) sort [ rank>value swap rank>value swap - -1 = ] monotonic? ; : straight-flush? ( hand -- ? ) [ straight? ] [ flush? ] bi and ; : two-pair? ( hand -- ? ) <permutations> [ [ [ first ] [ second ] bi =-rank ] [ [ third ] [ fourth ] bi =-rank ] bi and ] any? ; : keep-not-eq ( <=> <=> -- <=> ) swap dup +eq+ = not [ nip ] [ drop ] if ; : cmp-same-hands ( hand hand -- <=> ) [ rank-ordered-rank ] bi@ [ <=> ] [ keep-not-eq ] 2map-reduce ; : cmp-step ( hand hand ? ? quote -- <=> ) -rot 2dup and [ 3drop cmp-same-hands ] [ 2dup [ not ] bi@ and [ 2drop call( hand hand -- <=> ) ] [ [ 4drop ] dip [ +lt+ ] [ +gt+ ] if ] if ] if ; : cmp-pair ( hand hand -- <=> ) 2dup [ pair? ] bi@ [ cmp-same-hands ] cmp-step ; : cmp-two-pair ( hand hand -- <=> ) 2dup [ two-pair? ] bi@ [ cmp-pair ] cmp-step ; : cmp-three-kind ( hand hand -- <=> ) 2dup [ three-of-kind? ] bi@ [ cmp-two-pair ] cmp-step ; : cmp-straight ( hand hand -- <=> ) 2dup [ straight? ] bi@ [ cmp-three-kind ] cmp-step ; : cmp-flush ( hand hand -- <=> ) 2dup [ flush? ] bi@ [ cmp-straight ] cmp-step ; : cmp-full-house ( hand hand -- <=> ) 2dup [ full-house? ] bi@ [ cmp-flush ] cmp-step ; : cmp-four-kind ( hand hand -- <=> ) 2dup [ four-of-kind? ] bi@ [ cmp-full-house ] cmp-step ; : cmp-hands ( hand hand -- <=> ) 2dup [ straight-flush? ] bi@ [ cmp-four-kind ] cmp-step ; : keep-best-hand ( hand hand -- hand ) 2dup cmp-hands +gt+ = [ drop ] [ nip ] if ; : num-wins ( -- assoc ) H{ } ; : clear-num-wins ( -- ) num-wins keys [ num-wins delete-at ] each ; : best-com-hand ( com hand -- hand ) append 5 <combinations> dup first [ keep-best-hand ] reduce ; : remove-seq ( seq seq -- seq ) [ swap remove ] each ; : remove-used-cards ( com hands discard deck -- com hands deck ) swap remove-seq [ 2dup ] dip swap [ remove-seq ] each swap remove-seq ; : ident ( -- ident ) "tie" worst-hand 2array ; : add-wins ( hands besthands -- ) [ 2array ] 2map ident [ 2dup [ second ] bi@ cmp-hands dup +eq+ = [ 3drop ident ] [ +gt+ = [ drop ] [ nip ] if ] if ] reduce first num-wins [ dup [ 1 + ] [ drop 1 ] if ] change-at ; : eval-hands ( com hands -- ) tuck [ over best-com-hand ] map nip add-wins ; : extend ( com hands deck -- ) rot dup length 5 < [ over [ [ 3dup ] dip dup [ 1array append ] dip rot remove swapd extend ] each 3drop ] [ rot eval-hands drop ] if ; recursive : total-wins ( -- x ) 0 num-wins keys [ num-wins at + ] each ; : output-wins ( -- ) num-wins keys [ dup [ print-card ] each num-wins at total-wins / 100 * >float . ] each ; : calculate-probability ( com hands discard -- ) deck remove-used-cards extend output-wins clear-num-wins ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | def stripList(lst): return [x for x in lst if not x is None] def endState(): for x, col in enumerate(board): for y, space in enumerate(col): if space != 0: if (x + 3 < len(board) and space == board[x+1][y] == board[x+2][y] == board[x+3][y]): return space if (y + 3 < len(board[0]) and space == board[x][y+1] == board[x][y+2] == board[x][y+3]): return space if (x + 3 < len(board) and y + 3 < len(board[0]) and space == board[x+1][y+1] == board[x+2][y+2] == board[x+3][y+3]): return space if (x + 3 < len(board) and y - 3 >= 0 and space == board[x+1][y-1] == board[x+2][y-2] == board[x+3][y-3]): return space return 0 def updateBoard(move, player): moveStack.append(move) if player == max: board[move][board[move].index(0)] = 1 else: board[move][board[move].index(0)] = -1 def undo(): move = moveStack.pop() if 0 in board[move]: board[move][board[move].index(0) - 1] = 0 else: board[move][-1] = 0 def check(arr): return 0 in arr and ((1 in arr) ^ (-1 in arr)) def evaluate4(arr): if 1 in arr: return 10 ** (arr.count(1) - 1) return -(10 ** (arr.count(-1) - 1)) def evaluateBoard(): if (winner := endState()) != 0: return winner * 100000 total = 0 for x, col in enumerate(board): for y, space in enumerate(col): if (x + 3 < len(board)): right = [space, board[x+1][y], board[x+2][y], board[x+3][y]] if check(right): total += evaluate4(right) if (y + 3 < len(board[0])): up = [space, board[x][y+1], board[x][y+2], board[x][y+3]] if check(up): total += evaluate4(up) if (x + 3 < len(board) and y + 3 < len(board[0])): upright = [space, board[x+1][y+1], board[x+2][y+2], board[x+3][y+3]] if check(upright): total += evaluate4(upright) if (x + 3 < len(board) and y - 3 >= 0): downright = [space, board[x+1][y-1], board[x+2][y-2], board[x+3][y-3]] if check(downright): total += evaluate4(downright) return total def evaluateMove(move, player, depth = 0): global board if not 0 in board[move]: return None updateBoard(move, min if player == max else max) if (winner := endState()) != 0: returnVal = winner * 100000 elif depth > 4: #tune this if it takes to long/short returnVal = evaluateBoard() else: moves = [evaluateMove(x, min if player == max else max, depth+1) for x in range(7)] if all(move is None for move in moves): returnVal = None else: returnVal = player(stripList(moves)) undo() return returnVal def move(player): moves = [evaluateMove(x, min if player == max else max) for x in range(7)] choosenMove = moves.index(player(stripList(moves))) updateBoard(choosenMove, player) return choosenMove board = [[0 for _ in range(6)] for _ in range(7)] moveStack = [] side = input() player = max if side == "f": player = min print(move(player) + 1) while endState() == 0: updateBoard(int(input()) - 1, min if player == max else max) print(move(player) + 1) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | def fib(n): a = 0 b = 1 if n < 0: return -fib(-n) elif n == 0: return 0 elif n == 1: return 1 else: for i in range(1, n): a, b = b, a + b return b def closest_pos(i): upper = 1 while fib(upper) <= i: upper = upper * 2 lower = int(upper/2) while upper-lower != 1: mid = int((lower+upper)/2) if fib(mid) <= i: lower = mid else: upper = mid return (lower, upper) dic = {} def fib_add(i): if not i in dic: dic[i] = fib_add_(i) return dic[i] def fib_add_(i): if i == 1: return [1] if i == 0: return [] smaller, larger = closest_pos(i) smaller = [smaller] + fib_add(i - fib(smaller)) larger = [larger] + [-x for x in fib_add(-(i - fib(larger)))] if len(smaller) < len(larger): return smaller else: return larger |
post a comment