-
Notifications
You must be signed in to change notification settings - Fork 28
Task 02: git task #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marynamalakhova
wants to merge
4
commits into
Kernel-GL-HRK:Maryna.Malakhova
Choose a base branch
from
marynamalakhova:task_scissors
base: Maryna.Malakhova
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d8ec6b1
SCISSORS: Add Makefile and main
marynamalakhova 42eaa77
SCISSORS: Add reading users input and it`s convertion to text
marynamalakhova dcf92da
SCISSORS: Add random choise of the computers' decision
marynamalakhova 889a3cc
SCISSORS: Add game and Finish task
marynamalakhova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| CC=gcc | ||
| SOURCES=main.c scissors.c | ||
| EXECUTABLE=scissors_game | ||
|
|
||
| all: $(SOURCES) | ||
| $(CC) $(SOURCES) -o $(EXECUTABLE) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // SPDX-License-Identifier: GPL-2.0 | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include "scissors.h" | ||
|
|
||
| static const char * const Game_Elements[] = { | ||
| "rock", "paper", "scissors", "unknown"}; | ||
|
|
||
| int main(void) | ||
| { | ||
| char input; | ||
| int res; | ||
| int comp_choise; | ||
|
|
||
| init_scissors(); | ||
| do { | ||
| printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); | ||
| scanf("%1s", &input); | ||
| res = char_to_element_index(input); | ||
| if (res == UNKNOWN) { | ||
| printf("You've chosen something strange!\n"); | ||
| continue; | ||
| } | ||
| comp_choise = play_scissors(); | ||
| printf("You choose %s, I choose %s\n", | ||
| Game_Elements[res], Game_Elements[comp_choise]); | ||
| switch (get_game_result(res, comp_choise)) { | ||
| case LOST: | ||
| printf("I win: %s beats %s\n", | ||
| Game_Elements[comp_choise], Game_Elements[res]); | ||
| break; | ||
| case WINN: | ||
| printf("You win: %s beats %s\n", | ||
| Game_Elements[res], Game_Elements[comp_choise]); | ||
| break; | ||
| case DRAW: | ||
| printf("It's draw\n"); | ||
| break; | ||
| } | ||
| } while (1); | ||
| } | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // SPDX-License-Identifier: GPL-2.0 | ||
| #include <stdlib.h> | ||
| #include <time.h> | ||
| #include "scissors.h" | ||
|
|
||
| /* | ||
| * Table indicates user's lost or win | ||
| * The row index corresponds to user's choise | ||
| * The column index corresponds to comp's choice | ||
| */ | ||
|
|
||
| static const int game_table[3][3] = { | ||
| /* rock paper scissors */ | ||
| /* rock*/ DRAW, LOST, WINN, | ||
| /* paper*/ WINN, DRAW, LOST, | ||
| /* sciss*/ LOST, WINN, DRAW | ||
| }; | ||
|
|
||
| int char_to_element_index(char c) | ||
| { | ||
| int ans = UNKNOWN; | ||
|
|
||
| switch (c) { | ||
| case 'r': | ||
| ans = ROCK; | ||
| break; | ||
| case 'p': | ||
| ans = PAPER; | ||
| break; | ||
| case 's': | ||
| ans = SCISSORS; | ||
| break; | ||
| } | ||
| return ans; | ||
| } | ||
|
|
||
| void init_scissors(void) | ||
| { | ||
| srand(time(NULL)); | ||
| } | ||
|
|
||
| int play_scissors(void) | ||
| { | ||
| return (rand()%3); | ||
| } | ||
|
|
||
| int get_game_result(int user, int comp) | ||
| { | ||
| return game_table[user][comp]; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitions order in source file and declarations order in header should be aligned. |
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // SPDX-License-Identifier: GPL-2.0 | ||
|
|
||
| #define ROCK 0 | ||
| #define PAPER 1 | ||
| #define SCISSORS 2 | ||
| #define UNKNOWN 3 | ||
| #define DRAW 0 | ||
| #define LOST 1 | ||
| #define WINN 2 | ||
|
|
||
| int char_to_element_index(char c); | ||
| void init_scissors(void); | ||
| int play_scissors(void); | ||
| int get_game_result(int user, int comp); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.