Last active
May 31, 2024 12:22
-
-
Save DanielAugusto191/2f6107c4f7ed06be895522cdee48f207 to your computer and use it in GitHub Desktop.
Bitwise Rock-Paper-Scissor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdint> | |
| #include <iostream> | |
| #include <bitset> | |
| using namespace std; | |
| void use_bitset(void){ | |
| bitset<3> p1b{"001"}; // Player 1 Hand | |
| bitset<3> p2b{"010"}; // Player 2 Hand | |
| bitset<9> game; | |
| bitset<9> p1w{"001100010"}; // Stats such that player 1 wins; | |
| bitset<9> p2w{"010001100"}; // Stats such that player 2 wins; | |
| bitset<3> result; // player1, player2, draw; | |
| for(int i=0;i<3;++i) for(int j=0;j<3;++j) game[3*i+j] = p1b[i] && p2b[j]; | |
| for(int i=0;i<9;++i) result[0] = result[0] || (p1w[i] && game[i]); // P1 wins | |
| for(int i=0;i<9;++i) result[1] = result[1] || (p2w[i] && game[i]); // P2 wins | |
| result[2] = !(result[0] || result[1]); // Draw | |
| cout << (result[0]?"Player1":(result[1]?"Player2":"Draw")) << endl; | |
| } | |
| int main(void){ | |
| bool p1b[3] = {0,0,1}; | |
| bool p2b[3] = {0,1,0}; | |
| bool ansp[9]; | |
| bool p1won[9] = { | |
| 0,0,1, | |
| 1,0,0, | |
| 0,1,0 | |
| }; | |
| bool p2won[9] = { | |
| 0,1,0, | |
| 0,0,1, | |
| 1,0,0 | |
| }; | |
| bool r1 = 0, r2 = 0, r3 = 0; | |
| for(int i=0;i<3;++i) for(int j=0;j<3;++j) ansp[3*i+j] = p1b[i] && p2b[j]; | |
| for(int i=0;i<9;++i) r1 |= (p1won[i] && ansp[i]); | |
| for(int i=0;i<9;++i) r2 |= (p2won[i] && ansp[i]); | |
| r3 = !(r1 || r2); | |
| cout << (r1?"Player1":(r2?"Player2":"Draw")) << endl; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment