A week ago I made [a short review](https://steemit.com/bitcoin/@dragosroua/experimenting-with-machine-payable-apis-my-early-review-of-the-21-app) of a new site called 21.co. It's one of the most hyped Bitcoin startups, the makers of the first Bitcoin computer (a glorified Raspberry PI with an ASIC miner on top of it, basically). The Bitcoin computer didn't convince me, but their API did. To make a long story short, the platform created by 21.co _enables anyone to create machine-payable APIs_. In mundane terms, you can monetize, with Bitcoin, anything that sits behind a URL: a file, an image, a membership, a game. What follows is a very simple - but fully functional - game made with this API. It's a version of the popular Rock, Paper, Scissor game, in which the opponent is the server. # Prerequisites You need to open an account at [21.co](https:/21.co), obviously. You will also need to download, install and configure (by logging yourself in) their SDK. Fortunately, they made this super easy, just visit this URL and you'll be set in minutes (or at least, that's how it took for me). https://21.co/features/ # Game Architecture The game contains 2 modules, a server and a client. The server is responsible for making and storing a choice (rock, paper or scissors) and the client is responsible for sending its choice. If both are choosing the same thing, the server issue the message "Tie!". If choices are different, then the winner is picked based on the Rock, Paper, Scissor traditional game mechanic (paper covers rock, scissors break paper, etc). For each call made by the client, a certain payment is made. If the client loses, there is no other payment incurred. If the client wins, it gets a small prize back, bigger than the amount invested. # Source Code You can download the entire source code from [this Github repo](https://github.com/dragosroua/21app-rock-paper-scissors). We will have a short look at both files in order to understand what's going on. The code is commented so it should be relatively easy. **rock-paper-scissors.py** ```#!/usr/bin/env python3 import random from flask import Flask from flask import request from two1.wallet import Wallet from two1.bitserv.flask import Payment app = Flask(__name__) wallet = Wallet() payment = Payment(app, wallet) choices_array = { 'Rock': '1', 'Paper': '2', 'Scissors': '3' } choices_list = list(choices_array.keys()) server_choice = random.choice(choices_list) # endpoint to get the server's choice @app.route('/choice') def get_choice(): return server_choice # machine-payable endpoint that checks user's answer @app.route('/play') @payment.required(3000) def pick_choice(): # extract answer from client request answer = request.args.get('selection') # extract payout address from client address client_payout_addr = request.args.get('payout_address') # check if answer is correct # logic for determiining the winner if answer == '1': if choices_array[server_choice] == '1': return "Tie!" else: if choices_array[server_choice] == '2': return "Server chose Paper. Server wins, Paper covers Rock!" else: return "Server chose Scissors. You win, Rock crushes Scissors!" wallet.send_to(client_payout_addr, 5000, True, 1000) elif answer == '2': if choices_array[server_choice] == '2': return "Tie!" else: if choices_array[server_choice] == '1': return "Server chose Rock. You win, Paper covers Rock!" wallet.send_to(client_payout_addr, 5000, True, 1000) else: return "Server chose Scissors. Server wins, Scissors breaks Paper!" elif answer == '3': if choices_array[server_choice] == '3': return "Tie!" else: if choices_array[server_choice] == '2': return "Server chose Paper. You win, Scissors breaks Paper!" wallet.send_to(client_payout_addr, 5000, True, 1000) else: return "Server chose Rock. Server wins, Rock crushes Scissors!" else: return "Incorrect response." if __name__ == '__main__': app.run(host='::', debug=True) ``` *** After we import the required dependencies, we define an array in which we hold the 3 options. We also generate a random choice and store it. The heavy lifting part is generated by this line: ``` @app.route('/play') @payment.required(3000) ``` *** The ```play``` endpoint will issue a ```402 Payment Required``` header, which will ask for 3000 satoshi. Upon completion of the transaction, the server will get the user's input and decide the winner. If the client wins, the server sends back 5000 satoshi. Here's how the play file looks like: **play.py** ``` #!/usr/bin/env python3 from two1.wallet import Wallet from two1.bitrequests import BitTransferRequests # set up bitrequest client for BitTransfer requests wallet = Wallet() requests = BitTransferRequests(wallet) # server address server_url = 'http://[::1]:5000/' def play(): # show the selection to the user, waiting for input ans = input("Your choice? Rock: 1, Paper: 2, Scissors: 3\n") sel_url = server_url + 'play?selection={0}&payout_address={1}' answer = requests.get(url=sel_url.format(ans, wallet.get_payout_address())) print(answer.text) if __name__ == '__main__': play() ``` *** As you see, it's a very simple interface, in which the user is asked to make his pick. Here's an example screenshot: ***  *** If you want to see the transactions, issue the command ```21 log```. The output should look like this: ***  *** # Troubleshooting It is possible that you will raise an exception if you don't have enough bitcoin in your local wallet. Please complete all the introductory tasks at the 21.co website after the signup, that will give you around $2 worth of bitcoin to play with. Please note there are two sources of bitcoin in the 21.co setup: off-chain and on-chain (there are even payment channels, but this is too complex for this article). The off-chain balance is what you get when you complete the assignments in the 21.co website and there is an withdrawal limit if you want to take that out and make it available on-chain (the main Bitcoin network), you can't withdraw less than $2 worth of bitcoin. # Potential The 21.co API is more complex than that. I just wanted to showcase what you can do extremely fast in order to create a bitcoin-powered API. There are many examples at the 21.co website and some of them are mind-boggling. Feedback, criticism, suggestions? *** _I'm a serial entrepreneur, blogger and ultrarunner. You can find me mainly on my blog at [Dragos Roua](http://dragosroua.com) where I write about productivity, business, relationships and running. Here on Steemit you may stay updated by following me @dragosroua._<center>  </center> *** <center>_**You can also vote for me as a Steemit witness here: https://steemit.com/~witnesses**_</center>
author | dragosroua |
---|---|
permlink | tutorial-create-a-rock-paper-scissors-bitcoin-game-in-python-using-21-app-machine-payable-api |
category | bitcoin |
json_metadata | {"tags":["bitcoin","python","game","app21"],"users":["dragosroua"],"image":["http://dragosroua.com/wp-content/uploads/2017/02/Screen-Shot-2017-02-09-at-11.33.25-AM.png","http://dragosroua.com/wp-content/uploads/2017/02/Screen-Shot-2017-02-09-at-11.35.14-AM.png","http://dragosroua.com/wp-content/uploads/2014/05/bamf-e1448824881306.png"],"links":["https://steemit.com/bitcoin/@dragosroua/experimenting-with-machine-payable-apis-my-early-review-of-the-21-app","https:/21.co","https://21.co/features/","https://github.com/dragosroua/21app-rock-paper-scissors","http://dragosroua.com","https://steemit.com/~witnesses"],"app":"steemit/0.1","format":"markdown"} |
created | 2017-02-09 09:44:27 |
last_update | 2017-02-09 09:44:27 |
depth | 0 |
children | 4 |
last_payout | 2017-03-12 10:28:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 7.920 HBD |
curator_payout_value | 0.915 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 7,361 |
author_reputation | 372,798,229,806,288 |
root_title | "Tutorial: Create A 'Rock, Paper, Scissors' Bitcoin Game in Python Using 21 app Machine-Payable API" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 2,461,185 |
net_rshares | 31,169,093,402,538 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
barrie | 0 | 624,855,383,255 | 100% | ||
berniesanders | 0 | 4,357,215,953,133 | 50% | ||
xeldal | 0 | 8,038,617,698,289 | 100% | ||
enki | 0 | 4,818,961,488,805 | 100% | ||
sandra | 0 | 50,527,563,214 | 90% | ||
silver | 0 | 305,146,287,588 | 50% | ||
nextgencrypto | 0 | 1,866,208,782,342 | 50% | ||
wang | 0 | 912,266,062,303 | 60% | ||
steemservices | 0 | 17,430,201,549 | 3% | ||
aizensou | 0 | 695,799,548,238 | 100% | ||
mineralwasser | 0 | 1,669,141,390 | 100% | ||
bingo-1 | 0 | 2,395,350,677 | 100% | ||
proctologic | 0 | 1,358,267,456 | 2% | ||
craig-grant | 0 | 618,301,063,955 | 100% | ||
ervin-lemark | 0 | 30,923,150,093 | 100% | ||
murh | 0 | 1,357,046,695 | 10% | ||
taoteh1221 | 0 | 6,318,437,263 | 100% | ||
applecrisp | 0 | 291,174,213 | 10% | ||
kenny-crane | 0 | 119,250,629,619 | 100% | ||
svamiva | 0 | 36,809,709,239 | 100% | ||
fyrstikken | 0 | 56,429,913,766 | 1% | ||
grey580 | 0 | 372,977,784 | 4% | ||
elyaque | 0 | 144,884,969,479 | 100% | ||
slowwalker | 0 | 669,084,636,133 | 100% | ||
furion | 0 | 7,163,033,275 | 1% | ||
vi1son | 0 | 36,062,288,412 | 100% | ||
speda | 0 | 42,920,135,283 | 100% | ||
gregm | 0 | 171,433,831,858 | 100% | ||
shla-rafia | 0 | 5,560,014,952 | 11% | ||
fiona777 | 0 | 5,989,798,238 | 100% | ||
deanliu | 0 | 63,079,460,262 | 100% | ||
celsius100 | 0 | 43,316,040,484 | 100% | ||
exyle | 0 | 305,432,357,899 | 100% | ||
sokoloffa | 0 | 6,474,132,373 | 100% | ||
marius19 | 0 | 127,877,581,454 | 100% | ||
alex.chien | 0 | 1,938,151,415 | 100% | ||
cmp2020 | 0 | 27,537,363,032 | 63% | ||
laoyao | 0 | 28,809,463,042 | 100% | ||
accumulator | 0 | 1,658,388,431 | 100% | ||
oflyhigh | 0 | 16,678,150,325 | 100% | ||
hms818 | 0 | 2,065,529,999 | 100% | ||
borran | 0 | 12,945,320,232 | 100% | ||
matrixdweller | 0 | 491,164,160 | 1% | ||
chinadaily | 0 | 72,189,142,622 | 100% | ||
helene | 0 | 47,971,964,564 | 100% | ||
lemouth | 0 | 61,207,696,662 | 100% | ||
neptun | 0 | 313,119,170,566 | 100% | ||
cryptomancer | 0 | 149,508,262,853 | 100% | ||
steevc | 0 | 30,583,770,531 | 100% | ||
creatr | 0 | 53,874,580,527 | 100% | ||
ivet | 0 | 13,342,020,561 | 51% | ||
cornerstone | 0 | 225,429,072,272 | 80% | ||
thecyclist | 0 | 1,368,187,834,450 | 50% | ||
kyusho | 0 | 31,382,183,243 | 100% | ||
steemit79 | 0 | 50,449,176 | 100% | ||
funnyman | 0 | 51,123,863,262 | 100% | ||
saamychristen | 0 | 8,041,460,435 | 100% | ||
arama | 0 | 2,024,469,953,621 | 100% | ||
drac59 | 0 | 4,228,544,564 | 100% | ||
dodders007 | 0 | 36,851,198,770 | 100% | ||
cub1 | 0 | 8,967,661,126 | 90% | ||
food-creator | 0 | 6,608,747,830 | 100% | ||
curiesea | 0 | 6,904,453,222 | 100% | ||
lalala | 0 | 290,662,489 | 100% | ||
alcibiades | 0 | 25,906,734,624 | 100% | ||
dragosroua | 0 | 112,868,333,957 | 100% | ||
steemspeak | 0 | 232,903,464 | 1% | ||
tablettenformat | 0 | 1,566,446,332 | 100% | ||
fyrst-witness | 0 | 377,696,998 | 1% | ||
raluca | 0 | 11,090,778,820 | 100% | ||
angel76 | 0 | 10,690,235,776 | 100% | ||
sqube | 0 | 2,958,297,768 | 1% | ||
crowdfundedwhale | 0 | 25,648,532,358 | 50% | ||
breezin | 0 | 9,815,018,520 | 100% | ||
teofilex11 | 0 | 11,668,087,687 | 100% | ||
goldsteem | 0 | 45,066,520,903 | 90% | ||
reisman | 0 | 483,772,640 | 10% | ||
engagement | 0 | 2,004,842,931,265 | 50% | ||
generikat | 0 | 22,706,690,689 | 100% | ||
pavan.burlagadda | 0 | 10,366,948,007 | 66% | ||
codydeeds | 0 | 47,749,665,762 | 100% | ||
madlenfox | 0 | 153,121,932 | 100% | ||
josehurtado | 0 | 1,609,006,508 | 100% | ||
triddin | 0 | 5,946,625,544 | 10% | ||
nomadsteem | 0 | 5,487,022,977 | 100% | ||
birrulibmc | 0 | 1,354,359,173 | 100% | ||
firesteem | 0 | 1,326,145,211 | 100% | ||
fisteganos | 0 | 3,673,461,764 | 50% | ||
azlicr | 0 | 400,683,395 | 100% | ||
jfaneumann | 0 | 2,118,155,597 | 100% | ||
ogochukwu | 0 | 1,290,382,671 | 100% | ||
smalltalk | 0 | 1,015,194,358 | 100% | ||
blacklist | 0 | 208,591,759 | 1% | ||
davidgermano | 0 | 1,380,772,965 | 70% | ||
owtblockchain | 0 | 364,793,347 | 100% | ||
adminpoly | 0 | 127,749,335 | 30% | ||
emeka | 0 | 357,405,482 | 100% |
This is indeed *very* cool stuff... I'm still shy of the fine print at http://21.co though... Thanks a lot for the neat example!
author | creatr |
---|---|
permlink | re-dragosroua-tutorial-create-a-rock-paper-scissors-bitcoin-game-in-python-using-21-app-machine-payable-api-20170210t073309727z |
category | bitcoin |
json_metadata | {"tags":["bitcoin"],"links":["http://21.co"],"app":"steemit/0.1"} |
created | 2017-02-10 07:33:09 |
last_update | 2017-02-10 07:33:09 |
depth | 1 |
children | 0 |
last_payout | 2017-03-12 10:28:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 130 |
author_reputation | 136,627,187,742,915 |
root_title | "Tutorial: Create A 'Rock, Paper, Scissors' Bitcoin Game in Python Using 21 app Machine-Payable API" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 2,469,372 |
net_rshares | 0 |
The example you picked looks like a slot machine to me :)
author | lemouth |
---|---|
permlink | re-dragosroua-tutorial-create-a-rock-paper-scissors-bitcoin-game-in-python-using-21-app-machine-payable-api-20170209t131123140z |
category | bitcoin |
json_metadata | {"tags":["bitcoin"],"app":"steemit/0.1"} |
created | 2017-02-09 13:11:24 |
last_update | 2017-02-09 13:11:24 |
depth | 1 |
children | 2 |
last_payout | 2017-03-12 10:28:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 57 |
author_reputation | 338,011,164,701,274 |
root_title | "Tutorial: Create A 'Rock, Paper, Scissors' Bitcoin Game in Python Using 21 app Machine-Payable API" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 2,462,392 |
net_rshares | 0 |
Almost, but not. You gave me a nice idea, though :)
author | dragosroua |
---|---|
permlink | re-lemouth-re-dragosroua-tutorial-create-a-rock-paper-scissors-bitcoin-game-in-python-using-21-app-machine-payable-api-20170209t160453213z |
category | bitcoin |
json_metadata | {"tags":["bitcoin"],"app":"steemit/0.1"} |
created | 2017-02-09 16:04:45 |
last_update | 2017-02-09 16:04:45 |
depth | 2 |
children | 1 |
last_payout | 2017-03-12 10:28:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 51 |
author_reputation | 372,798,229,806,288 |
root_title | "Tutorial: Create A 'Rock, Paper, Scissors' Bitcoin Game in Python Using 21 app Machine-Payable API" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 2,463,613 |
net_rshares | 0 |
Ahaha :)
author | lemouth |
---|---|
permlink | re-dragosroua-re-lemouth-re-dragosroua-tutorial-create-a-rock-paper-scissors-bitcoin-game-in-python-using-21-app-machine-payable-api-20170210t211838936z |
category | bitcoin |
json_metadata | {"tags":["bitcoin"],"app":"steemit/0.1"} |
created | 2017-02-10 21:18:39 |
last_update | 2017-02-10 21:18:39 |
depth | 3 |
children | 0 |
last_payout | 2017-03-12 10:28:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 8 |
author_reputation | 338,011,164,701,274 |
root_title | "Tutorial: Create A 'Rock, Paper, Scissors' Bitcoin Game in Python Using 21 app Machine-Payable API" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 2,474,537 |
net_rshares | 0 |