I am new to C. I am experienced in GWBASIC. In an effort to learn, I am attempting to write a program that will convert the individual chars in a string to a numerical value as so:
1 2 3 4 5 6 7 8 9
a b c d e f g h i
j k l m n o p q r
s t u v w x y z
For example, user input for string A could be 'dog',
said program would then store [d][o][g] as [4][6][7] in string B.
The below code works for a string w/up to four chars, but there must be a more efficient way of doing this.
int main()
{
char a[0];
char b[0];
scanf("%s",a);
if (a[0] == 'a' || a[0] == 'j' || a[0] == 's') b[0] = '1';
if (a[0] == 'b' || a[0] == 'k' || a[0] == 't') b[0] = '2';
if (a[0] == 'c' || a[0] == 'l' || a[0] == 'u') b[0] = '3';
if (a[0] == 'd' || a[0] == 'm' || a[0] == 'v') b[0] = '4';
if (a[0] == 'e' || a[0] == 'n' || a[0] == 'w') b[0] = '5';
if (a[0] == 'f' || a[0] == 'o' || a[0] == 'x') b[0] = '6';
if (a[0] == 'g' || a[0] == 'p' || a[0] == 'y') b[0] = '7';
if (a[0] == 'h' || a[0] == 'q' || a[0] == 'z') b[0] = '8';
if (a[0] == 'i' || a[0] == 'r') b[0] = '9';
if (a[1] == 'a' || a[1] == 'j' || a[1] == 's') b[1] = '1';
if (a[1] == 'b' || a[1] == 'k' || a[1] == 't') b[1] = '2';
if (a[1] == 'c' || a[1] == 'l' || a[1] == 'u') b[1] = '3';
if (a[1] == 'd' || a[1] == 'm' || a[1] == 'v') b[1] = '4';
if (a[1] == 'e' || a[1] == 'n' || a[1] == 'w') b[1] = '5';
if (a[1] == 'f' || a[1] == 'o' || a[1] == 'x') b[1] = '6';
if (a[1] == 'g' || a[1] == 'p' || a[1] == 'y') b[1] = '7';
if (a[1] == 'h' || a[1] == 'q' || a[1] == 'z') b[1] = '8';
if (a[1] == 'i' || a[1] == 'r') b[1] = '9';
if (a[2] == 'a' || a[2] == 'j' || a[2] == 's') b[2] = '1';
if (a[2] == 'b' || a[2] == 'k' || a[2] == 't') b[2] = '2';
if (a[2] == 'c' || a[2] == 'l' || a[2] == 'u') b[2] = '3';
if (a[2] == 'd' || a[2] == 'm' || a[2] == 'v') b[2] = '4';
if (a[2] == 'e' || a[2] == 'n' || a[2] == 'w') b[2] = '5';
if (a[2] == 'f' || a[2] == 'o' || a[2] == 'x') b[2] = '6';
if (a[2] == 'g' || a[2] == 'p' || a[2] == 'y') b[2] = '7';
if (a[2] == 'h' || a[2] == 'q' || a[2] == 'z') b[2] = '8';
if (a[2] == 'i' || a[2] == 'r') b[2] = '9';
if (a[3] == 'a' || a[3] == 'j' || a[3] == 's') b[3] = '1';
if (a[3] == 'b' || a[3] == 'k' || a[3] == 't') b[3] = '2';
if (a[3] == 'c' || a[3] == 'l' || a[3] == 'u') b[3] = '3';
if (a[3] == 'd' || a[3] == 'm' || a[3] == 'v') b[3] = '4';
if (a[3] == 'e' || a[3] == 'n' || a[3] == 'w') b[3] = '5';
if (a[3] == 'f' || a[3] == 'o' || a[3] == 'x') b[3] = '6';
if (a[3] == 'g' || a[3] == 'p' || a[3] == 'y') b[3] = '7';
if (a[3] == 'h' || a[3] == 'q' || a[3] == 'z') b[3] = '8';
if (a[3] == 'i' || a[3] == 'r') b[3] = '9';
printf("%s\n",b);
return 0;
}
Assuming that your compiler uses an ASCII encoding then you can use the following simple arithmetic to get your answer:
1 + (strA[i] - 'a') % 9
You really don't want to implement this with a long list of if statements or indeed a switch statement.
Naturally you will have input validation issues if you have non alphabetical characters, numeric characters, upper-case characters and so on. I presume you can simply ignore those for a learning exercise.
To correct your original approach, you need to do two things:
Use single quotes around character constants;
Use == to check for equality;
Terminate statements with ;.
... so your snippet becomes:
if (strA[0] == 'a')
strB[0] = '1';
if (strA[0] == 'b')
strB[0] = '2';
if (strA[0] == 'c')
strB[0] = '3';
You can type the following exactly into a gwbasic editor and it will solve your problem
10 INPUT A$
12 L = LEN(A$)
15 FOR T = 1 TO L
20 M$ = MID$(A$,T,1)
25 GOSUB 70
30 B$ = B$ + V$
35 NEXT T
40 PRINT B$
50 END
55 REM -----------------
70 REM - Subroutine to convert m$ into v$
72 X = ASC(M$) : REM this is the ascii value of m$ (eg. "a" = 97)
74 X = X - 96 : REM so that 97 becomes "1"
80 IF X > 9 THEN X = X - 9 : GOTO 80
90 V$ = STR$(X) : REM just converting to a string type variable
95 RETURN : REM takes you back to line 30 where this value is added to the
96 REM final resulting B$ - when I say added I mean a char added to a string
97 REM such that "APPL" + "E" = "APPLE"
98 REM ------------------------------------------ DONE
For ASCII, it would go somewhat like:
... make sure strB has enough space ...
for (i = 0; i < ... length of strA ... ; i++) {
/* you should always check your input is valid */
if (strA[i] >= 'a' && strB[i] <= 'z')
strB[i] = (strA[i] - 'a') % 9 + 1;
else
strB[i] = ... some default value ...
}
For EBCDIC:
for (i = 0; i < ... length of strA ... ; i++) {
/* you should always check your input is valid */
if (strA[i] >= 'a' && strB[i] <= 'r')
strB[i] = strA[i] & 0xF;
else if (strA[i] >= 's' && strB <= 'z')
strB[i] = (strA[i] & 0xF) - 1;
else
strB[i] = ... some default value ...
}
Have a closer look at an ASCII table. You'll see that all letters are encoded with some integer value. As a start, if you only have lower case letters, it's enough to substract the character code of 'a' from any letter to get what you want
int nr = strA[0] - 'a' + 1;
//now you'd need to convert back to a string; better: strB should be an array of integer
Also, = is the assignment operator; you need to use == to check for equality.
try this :)
int strB[MAX_LEN] = {0};
char *strA = malloc (MAX_LEN * sizeof(char));
int i,c = 0,x;
scanf("%s",strA);
for(i = 0 ; i<strlen(strA) ; i++){
x = strA[i] - 'a' + 1;
if(x >= 1 && x <= 9)
strB[c] = x;
else if(x <= 18){
strB[c] = x - 10;
else if(x <= 26){
strB[c] = x - 19;
if(x <= 26)
c++;
}
or you can use ninjalj approach in the for loop, if you aleady checked the input:
for(i=0 ; i<strlen(strA) ; i++){
strB[i] = (strA[i] - 'a') % 9 + 1;
}
or this :
for(i=0 ; i<strlen(strA) ; i++){
if(strA[i] >= 'a' && strA[i] <= 'z'){
strB[c] = (strA[i] - 'a') % 9 + 1;
c++;
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I'm a rusty c programmer. I created a tictactoe program in python and wanted to do the same in c. Unfortunately, the compiler I installed is running my much older attempt at a tic tac toe game and the online compilers seem to be stuck on the while loops that are involved in decision-making. The while loops are marked out by comments in the code below
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
int XO, E, turn = 1;
char A[9] = " ";
srand(time(0));
printf(" | | \n | | \n--------------\n | | \n | | \n--------------\n | | \n | | \n");
printf("Enter '1' for x and '-1' for o: ");
scanf("%d", &XO);
while(true)
{
if(turn == -XO)
{
do
{
E = rand() % 9;
}while(A[E] != " "); #Is stuck on this
if(-XO == -1)
{
A[E] = "o";
}
else
{
A[E] = "x";
}
turn *= -1;
}
else if(turn == XO)
{
do
{
printf("Enter the position you want to put yours in. 0 for top left. 1 for top middle. 3 for middle left. 4 for middle middle and so on...\n");
scanf("%d", &E);
}while(A[E] != " "); #Is stuck on this
if(XO == -1)
{
A[E] = "o";
}
else
{
A[E] = "x";
}
turn *= -1;
}
printf(" %c | %c | %c \n | | \n--------------\n %c | %c | %c \n | | \n--------------\n %c | %c | %c \n | | ", A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8]);
if (((A[0] == "o" && A[1] == "o" && A[2] == "o") || (A[3] == "o" && A[4] == "o" && A[5] == "o") || (A[6] == "o" && A[7] == "o" && A[8] == "o") || (A[0] == "o" && A[4] == "o" && A[8] == "o") || (A[2] == "o" && A[4] == "o" && A[6] == "o") && XO == 1) || ((A[0] == "x" && A[1] == "x" && A[2] == "x") || (A[3] == "x" && A[4] == "x" && A[5] == "x") || (A[6] == "x" && A[7] == "x" && A[8] == "x") || (A[0] == "x" && A[4] == "x" && A[8] == "x") || (A[2] == "x" && A[4] == "x" && A[6] == "x") && XO == -1))
{
printf("You won!");
break;
}
else if (((A[0] == "o" && A[1] == "o" && A[2] == "o") || (A[3] == "o" && A[4] == "o" && A[5] == "o") || (A[6] == "o" && A[7] == "o" && A[8] == "o") || (A[0] == "o" && A[4] == "o" && A[8] == "o") || (A[2] == "o" && A[4] == "o" && A[6] == "o") && XO == -1) || ((A[0] == "x" && A[1] == "x" && A[2] == "x") || (A[3] == "x" && A[4] == "x" && A[5] == "x") || (A[6] == "x" && A[7] == "x" && A[8] == "x") || (A[0] == "x" && A[4] == "x" && A[8] == "x") || (A[2] == "x" && A[4] == "x" && A[6] == "x") && XO == 1))
{
printf("You lost!");
break;
}
}
return 0;
}
Here's the version that's on the compiler I installed:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int XO, E, turn = 1;
char A[9] = " ";
srand(time(0));
printf(" | | \n | | \n--------------\n | | \n | | \n--------------\n | | \n | | ");
printf("Enter '1' for x and '-1' for o");
scanf("%d", &XO);
while(1)
{
if(turn == -XO)
{
do
{
E = rand() % 9;
}while(A[E] != " ");
if(-XO == -1)
{
A[E] = "o";
}
else
{
A[E] = "x";
}
turn *= -1;
}
else if(turn == XO)
{
do
{
printf("Enter the position you want to put yours in. 0 for top left. 1 for top middle. 3 for middle left. 4 for middle middle and so on...");
scanf("%d", &E);
}while(A[E] != " ");
if(XO == -1)
{
A[E] = "o";
}
else
{
A[E] = "x";
}
turn *= -1;
}
printf(" %c | %c | %c \n | | \n--------------\n %c | %c | %c \n | | \n--------------\n %c | %c | %c \n | | ", A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8]);
if (((A[0] == "o" && A[1] == "o" && A[2] == "o") || (A[3] == "o" && A[4] == "o" && A[5] == "o") || (A[6] == "o" && A[7] == "o" && A[8] == "o") || (A[0] == "o" && A[4] == "o" && A[8] == "o") || (A[2] == "o" && A[4] == "o" && A[6] == "o") && XO == 1) || ((A[0] == "x" && A[1] == "x" && A[2] == "x") || (A[3] == "x" && A[4] == "x" && A[5] == "x") || (A[6] == "x" && A[7] == "x" && A[8] == "x") || (A[0] == "x" && A[4] == "x" && A[8] == "x") || (A[2] == "x" && A[4] == "x" && A[6] == "x") && XO == -1))
{
printf("You won!");
break;
}
else if (((A[0] == "o" && A[1] == "o" && A[2] == "o") || (A[3] == "o" && A[4] == "o" && A[5] == "o") || (A[6] == "o" && A[7] == "o" && A[8] == "o") || (A[0] == "o" && A[4] == "o" && A[8] == "o") || (A[2] == "o" && A[4] == "o" && A[6] == "o") && XO == -1) || ((A[0] == "x" && A[1] == "x" && A[2] == "x") || (A[3] == "x" && A[4] == "x" && A[5] == "x") || (A[6] == "x" && A[7] == "x" && A[8] == "x") || (A[0] == "x" && A[4] == "x" && A[8] == "x") || (A[2] == "x" && A[4] == "x" && A[6] == "x") && XO == 1))
{
printf("You lost!");
break;
}
}
return 0;
}
First of all, there are quite a lot of warnings and logic operations to fix. Some people already provided fixes in comments that you should apply.
Your program does not handle a fully filled array A. If A happens to become full without any win, you end up with infinite loops where you put comments. I think this is the source of the bug.
Also, at the end of your main loop, when you look for any win or defeat, you do not look for any "vertical win" by verifying (A[0] == 'x') && (A[3] == 'x') && (A[6] == 'x') for example.
Finally, adding #include <time.h> will fix the implicit declaration warning.
I have to find the vowels in an input string and return the string of the vowels in reverse order.
Example input: "woUIW"
The expected output is "IUOo"
But I cannot use pointers, only recursion.
This is my attempt:
void letters_revers(char s[])
{
int i = 0,g,len,right,left;
char reversed[10];
len=strlen(reversed)-1; // get the length of the string
left = 0; // set left index at 0
right = len - 1; // set right index len - 1
while(i!='\0')
{
if (s[i] == 'a' || s[i] == 'A' || s[i] == 'e' || s[i] == 'E' || s[i] == 'i' || s[i] == 'I' || s[i] =='o' || s[i]=='O' || s[i] == 'u' || s[i] == 'U')
{ reversed[i]+=s[i];
for(g = len - 1; g >= 0; g--)
{
printf("%c", reversed[g]);
}
}
}i++;
}
I am trying to make a Tic-Tac-Toe game. I have written the code which I believe should work fine, but instead it is throwing logical errors.
#include <stdio.h>
#define size 9
char game_logic();
int game_win();
char array[size] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int main()
{
printf("\n\t\t Welcome to Tic-Tac-Toe game\n\n");
printf(" In this game, there will be two players taking turns for their moves\n\n");
printf("\t\t Player 1 = 'X' and Player 2 = 'O'\n\n");
for (int i = 0; i < size; i++)
{
printf("\t%c", array[i]);
if (array[i] == '1' || array[i] == '2' || array[i] == '4' ||
array[i] == '5' || array[i] == '7' || array[i] == '8')
{
printf(" |");
}
if (array[i] == '3' || array[i] == '6')
{
printf("\n -----------------------\n");
}
}
printf("\n\n");
do
{
game_logic();
} while (game_win() != 1 || game_win() != 2 || game_win != 3);
if (game_win() == 1)
{
printf("\nThe Player 1 has won the game\n");
}
else if (game_win() == 2)
{
printf("\nThe Player 2 has won the game\n");
}
else if (game_win() == 3)
{
printf("\nThe game is draw\n");
}
return 0;
}
char game_logic()
{
int n, m;
for (int i = 0; i < size; i++)
{
if (i % 2 == 0)
{
printf("Player 1, Please select the number = ");
scanf("%d", &n);
if (n > 0 && n < 10)
{
array[n] = 'X';
return array[n];
break;
}
}
else if (i % 2 != 0)
{
printf("Player 2, Please select the number = ");
scanf("%d", &m);
if (n > 0 && n < 10)
{
array[n] = 'O';
return array[n];
break;
}
}
}
}
int game_win()
{
game_logic();
if (array[0] == array[1] == array[2] == 'X' ||
array[2] == array[5] == array[8] == 'X' ||
array[6] == array[7] == array[8] == 'X' ||
array[0] == array[3] == array[6] == 'X' ||
array[0] == array[4] == array[8] == 'X' ||
array[2] == array[4] == array[6] == 'X' ||
array[1] == array[4] == array[7] == 'X' ||
array[3] == array[4] == array[5] == 'X')
{
return 1;
}
else
if (array[0] == array[1] == array[2] == 'O' ||
array[2] == array[5] == array[8] == 'O' ||
array[6] == array[7] == array[8] == 'O' ||
array[0] == array[3] == array[6] == 'O' ||
array[0] == array[4] == array[8] == 'O' ||
array[2] == array[4] == array[6] == 'O' ||
array[1] == array[4] == array[7] == 'O' ||
array[3] == array[4] == array[5] == 'O')
{
return 2;
}
else
{
return 3;
}
}
The basic plan was to execute game_logic which will decide how the game will run, it will take input from the user in the form of integers between 1 and 9. The number selected by the user will be replaced by X in the global array if player 1 inputs it, and will be replaced by O if player 2 selects it.
The results of game_logic is to be sent to the game_win which will determine if the game has been won or not by returning the values to the main function.
But when I ran the code, a loop occurs which says "Player 1, Please select the number = ", and this keeps on going by taking infinite input.
I have tried debugging it, but it is unable to help me. So can please suggest solutions for the logic made, or if my logic is correct.
The output:
Welcome to Tic-Tac-Toe game
In this game, there will be two players taking turns for their moves
Player 1 = 'X' and Player 2 = 'O'
1 | 2 | 3
-----------------------
4 | 5 | 6
-----------------------
7 | 8 | 9
Player 1, Please select the number = 3
Player 1, Please select the number = 2
Player 1, Please select the number = 3
Player 1, Please select the number = 4
Player 1, Please select the number = 3
Player 1, Please select the number = 2
Player 1, Please select the number = 2
Player 1, Please select the number = 3
Player 1, Please select the number = 4
and this keeps on going.
There are at least these major problems:
the test in the do ... while loop is incorrect: the loop iterates while (game_win() != 1 || game_win() != 2 || game_win != 3)... the third test compare the function name with 3 not return value of the function call, and even if you fix this problem, the test is always true since game_win() cannot be both 1 and 2 and 3. You should instead write:
do {
game_logic();
} while (game_win() != 0);
the tests in game_win() are incorrect: array[0] == array[1] == array[2] == 'X' should be written:
(array[0] == 'X' && array[1] == 'X' && array[2] == 'X')
game_win() does not properly test for a draw and never returns 0 in other cases.
if you call game_logic() in the loop, game_win() should not also call game_logic(). It would make sense for game_logic() to return the game_win() result, which would simplify the loop as:
while (game_logic())
continue;
the slot modified in game_logic() is incorrect: the number n is between 1 and 9, but the array elements start at 0 so you should modify array[n - 1].
the slot for player 2 is read into m, whereas the code uses n afterwards.
it would be more user friendly to redisplay the board before each turn.
Here is a modified version:
#include <stdio.h>
#define SIZE 9
int game_win(void);
int game_logic(int i);
char array[SIZE] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
void display_board(void) {
printf("\n ");
for (int i = 1; i <= SIZE; i++) {
printf(" %c", array[i - 1]);
if (i == 1 || i == 2 || i == 4 || i == 5 || i == 7 || i == 8) {
printf(" |");
}
if (i == 3 || i == 6) {
printf("\n -----------------------\n ");
}
}
printf("\n\n");
}
int game_win(void) {
if ((array[0] == 'X' && array[1] == 'X' && array[2] == 'X') ||
(array[3] == 'X' && array[4] == 'X' && array[5] == 'X') ||
(array[6] == 'X' && array[7] == 'X' && array[8] == 'X') ||
(array[0] == 'X' && array[3] == 'X' && array[6] == 'X') ||
(array[1] == 'X' && array[4] == 'X' && array[7] == 'X') ||
(array[2] == 'X' && array[5] == 'X' && array[8] == 'X') ||
(array[0] == 'X' && array[4] == 'X' && array[8] == 'X') ||
(array[2] == 'X' && array[4] == 'X' && array[6] == 'X')) {
return 1;
}
if ((array[0] == 'O' && array[1] == 'O' && array[2] == 'O') ||
(array[3] == 'O' && array[4] == 'O' && array[5] == 'O') ||
(array[6] == 'O' && array[7] == 'O' && array[8] == 'O') ||
(array[0] == 'O' && array[3] == 'O' && array[6] == 'O') ||
(array[1] == 'O' && array[4] == 'O' && array[7] == 'O') ||
(array[2] == 'O' && array[5] == 'O' && array[8] == 'O') ||
(array[0] == 'O' && array[4] == 'O' && array[8] == 'O') ||
(array[2] == 'O' && array[4] == 'O' && array[6] == 'O')) {
return 2;
}
for (int i = 1; i <= SIZE; i++) {
if (array[i - 1] == '0' + i)
return 0;
}
return 3;
}
int game_logic(int i) {
for (;;) {
char buf[32];
int player, n;
char mark;
if (i % 2 == 0) {
player = 1;
mark = 'X';
} else {
player = 2;
mark = 'O';
}
printf("Player %d, Please select the number = ", player);
if (!fgets(buf, sizeof buf, stdin)) {
/* end of file reached: abort */
return -1;
}
if (sscanf(buf, "%d", &n) != 1 || n < 1 || n > 9) {
printf("please enter a number between 1 and 9\n");
continue;
}
if (array[n - 1] != '0' + n) {
printf("slot %d is already played\n", n);
continue;
}
array[n - 1] = mark;
return game_win();
}
}
int main() {
printf("\n\t\t Welcome to Tic-Tac-Toe game\n\n");
printf(" In this game, there will be two players taking turns for their moves\n\n");
printf("\t\t Player 1 = 'X' and Player 2 = 'O'\n\n");
for (int i = 0; i < SIZE; i++) {
display_board();
if (game_logic(i) != 0)
break;
}
switch (game_win()) {
case 0:
printf("\nThe game was aborted\n");
break;
case 1:
display_board();
printf("\nThe Player 1 has won the game\n");
break;
case 2:
display_board();
printf("\nThe Player 2 has won the game\n");
break;
case 3:
display_board();
printf("\nThe game is draw\n");
break;
}
return 0;
}
In the loop inside the game_logic function, it returns after Player1 entered a valid number. And then game_logic is called in a loop, so i gets never increased and this causes the endless loop.
I am trying to write a function able to replace a specific character with another.
However.. the output is kinda weird.. that i don't understand.
The code i present can be compiled without errors:
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
char file_name[256];
FILE* fp;
int ch, i=0, l=0;
unsigned char buff[2048];
/* Save current attributes */
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
printf("Enter File Name: ");
scanf("%s", &file_name);
fp = fopen(file_name, "r+a");
printf("\n Printing file character by character..\n");
for ( ; ; )
{
l++;
ch = fgetc(fp);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
printf("%c", ch); buff[l] = ch;
SetConsoleTextAttribute(hConsole, saved_attributes);
if(ch == EOF) break;
}
printf("\n\n Converting..\n");
for( ; i<ftell(fp); i++)
{
if(buff[i] == '1') buff[i] = '8';
if(buff[i] == '2') buff[i] = '9';
if(buff[i] == '3') buff[i] = '0';
if(buff[i] == '4') buff[i] = '-';
if(buff[i] == '5') buff[i] = '=';
if(buff[i] == '6') buff[i] = 'q';
if(buff[i] == '7') buff[i] = 'w';
if(buff[i] == '8') buff[i] = 'e';
if(buff[i] == '9') buff[i] = 'r';
if(buff[i] == '0') buff[i] = 't';
if(buff[i] == 'q') buff[i] = 'y';
if(buff[i] == 'w') buff[i] = 'u';
if(buff[i] == 'e') buff[i] = 'i';
if(buff[i] == 'r') buff[i] = 'o';
if(buff[i] == 't') buff[i] = 'p';
if(buff[i] == 'y') buff[i] = '[';
if(buff[i] == 'u') buff[i] = ']';
if(buff[i] == 'i') buff[i] = 'a';
if(buff[i] == 'o') buff[i] = 's';
if(buff[i] == 'p') buff[i] = 'd';
if(buff[i] == 'a') buff[i] = 'f';
if(buff[i] == 's') buff[i] = 'g';
if(buff[i] == 'd') buff[i] = 'h';
if(buff[i] == 'f') buff[i] = 'j';
if(buff[i] == 'g') buff[i] = 'k';
if(buff[i] == 'h') buff[i] = 'l';
if(buff[i] == 'j') buff[i] = ';';
if(buff[i] == 'k') buff[i] = '\'';
if(buff[i] == 'l') buff[i] = '\\';
if(buff[i] == 'z') buff[i] = 'z';
if(buff[i] == 'x') buff[i] = 'z';
if(buff[i] == 'c') buff[i] = 'x';
if(buff[i] == 'v') buff[i] = 'c';
if(buff[i] == 'b') buff[i] = 'v';
if(buff[i] == 'n') buff[i] = 'b';
if(buff[i] == 'm') buff[i] = 'n';
}
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
printf("%s\n", buff);
SetConsoleTextAttribute(hConsole, saved_attributes);
printf("\n Saving..");
fclose(fp);
{
FILE* fp = fopen(file_name, "w");
fputs(buff, fp);
}
}
I am testing it with the following file:
f l k [j6] 0 e r k l x [k0] r u I o k v C [x8]
w t y u C v n C Q r y I z x [c9] e y x [x0]
r [zu] l k [l6] 0 e r j k [lq] t i z [kw] y [hi] [j6] 0 e r t
But the result i get is:
(; \ ' [;[] \ ; ' ' \ z ['\] ' ] I ' ' c C [z;]
] \ [ ] C c b C Q ' [ I z z [x'] ; [ z [z\]
' [z]] \ ' [\[] \ ; ' ; ' [\[] \ ; z [']] [ [\;] [;[] \ ; ' \я
What is going on here?
Your characters are getting replaced multiple times in the same cycle. Consider the case when buff[i] == '1'. Let's go through the statements in your loop and see what happens.
if(buff[i] == '1') buff[i] = '8'; // Now, buff[i] is '8'
if(buff[i] == '2') buff[i] = '9'; // Nothing happens
if(buff[i] == '3') buff[i] = '0'; // Nothing happens
if(buff[i] == '4') buff[i] = '-'; // wait for it...
if(buff[i] == '5') buff[i] = '=';
if(buff[i] == '6') buff[i] = 'q';
if(buff[i] == '7') buff[i] = 'w';
if(buff[i] == '8') buff[i] = 'e'; // Now, buff[i] is 'e'
// ...
if(buff[i] == 'e') buff[i] = 'i'; // Now, buff[i] is 'i'
// ...
if(buff[i] == 'i') buff[i] = 'a'; // Now, buff[i] is 'a'
// ... and so on
Note that this all happens within one cycle of the loop. So a '1' gets encoded as ';' instead of '8', which is probably what you wanted.
You will probably want to prefix all of those ifs (besides the first) with an else.
My question is for the if statement under the "checks for duplicate move" section of the code. Sometimes the if statement works, but sometimes it just doesn't do it. The if statement is supposed to check the input from the user and compare it to the character currently in the coordinate. If the character is a 'X' or 'O' then it should not overwrite the character currently in that part of the array, tells the user it was an invalid move, and subtracts 1 from counter so it is still either X's or O's turn.
#include <stdio.h>
struct coordinate{
int i;
int j;
};
//function that prints the tic-tac-toe board
int printTable( char array[3][3]){
printf(" X's first O's second\n");
printf("\n 0 1 2\n");
printf("\n | | \n");
printf(" 0 %c | %c | %c \n", array[0][0], array[0][1], array[0][2]);
printf(" ___|___|___\n");
printf(" | | \n");
printf(" 1 %c | %c | %c \n", array[1][0], array[1][1], array[1][2]);
printf(" ___|___|___\n");
printf(" | | \n");
printf(" 2 %c | %c | %c \n", array[2][0], array[2][1], array[2][2]);
printf(" | | \n\n");
return 0;
}
int main (void){
struct coordinate move;
char game;
int counter;
char ttt[3][3] = {
{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}
};
printf("\nWelcome to tic tac toe!\n");
//Checks if you want to play
printf("Would you like to play? y/n\n");
scanf("%c", &game);
//stops game by returning zero
if(game == 'n'){
printf("Have a pleasant day");
return 0;
}
//starts game! =)
if(game == 'y'){
for(counter = 1; counter <= 9; counter++){
printTable(ttt);
//asks player where they want to move
printf("Where would you like to move?");
scanf("%i %i", &move.i, &move.j);
//Checks for duplicate move
if( ttt[move.i][move.j] == 'O' || ttt[move.i][move.j] == 'X' ){
ttt[move.i][move.j] = ttt[move.i][move.j];
printf("\n\n\nInvalid move. Try again\n\n\n");
counter = counter - 1;
}
//O wins
if( ttt[0][0] == 'O' && ttt[0][1] == 'O' && ttt[0][2] == 'O' || ttt[1][0] == 'O' && ttt[1][1] == 'O' && ttt[1][2] == 'O' || ttt[2][0] == 'O' && ttt[2][1] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][0] == 'O' && ttt[2][0] == 'O' || ttt[0][1] == 'O' && ttt[1][1] == 'O' && ttt[2][1] == 'O' || ttt[0][2] == 'O' && ttt[1][2] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][1] == 'O' && ttt[2][2] == 'O' || ttt[2][0] == 'O' && ttt[1][1] == 'O' && ttt[0][2] == 'O'){
printf("O WINS!!!!!");
return 0;
}
//X wins
if( ttt[0][0] == 'X' && ttt[0][1] == 'X' && ttt[0][2] == 'X' || ttt[1][0] == 'X' && ttt[1][1] == 'X' && ttt[1][2] == 'X' || ttt[2][0] == 'X' && ttt[2][1] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][0] == 'X' && ttt[2][0] == 'X' || ttt[0][1] == 'X' && ttt[1][1] == 'X' && ttt[2][1] == 'X' || ttt[0][2] == 'X' && ttt[1][2] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][1] == 'X' && ttt[2][2] == 'X' || ttt[2][0] == 'X' && ttt[1][1] == 'X' && ttt[0][2] == 'X'){
printf("X WINS!!!!!");
return 0;
}
//X's turns
if( counter == 0 || counter == 2 ||counter == 4 ||counter == 6 ||counter == 8){
ttt[move.i][move.j] = 'X';
}
//Y's turns
if( counter == 1 || counter == 3 ||counter == 5 ||counter == 7 ){
ttt[move.i][move.j] = 'O';
}
//CATS GAME
if( counter == 9){
printf("Cat's Game =(");
return 0;
}
}
}
return 0;
}
First of all, you could just write that if it is Y's turn, the if statement could look like
if(counter%2 == 1)
and respectively for X's turn:
if(counter%2 == 0)
What's more, this part of the code
//O wins
if( ttt[0][0] == 'O' && ttt[0][1] == 'O' && ttt[0][2] == 'O' || ttt[1][0] == 'O' && ttt[1][1] == 'O' && ttt[1][2] == 'O' || ttt[2][0] == 'O' && ttt[2][1] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][0] == 'O' && ttt[2][0] == 'O' || ttt[0][1] == 'O' && ttt[1][1] == 'O' && ttt[2][1] == 'O' || ttt[0][2] == 'O' && ttt[1][2] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][1] == 'O' && ttt[2][2] == 'O' || ttt[2][0] == 'O' && ttt[1][1] == 'O' && ttt[0][2] == 'O'){
printf("O WINS!!!!!");
return 0;
}
//X wins
if( ttt[0][0] == 'X' && ttt[0][1] == 'X' && ttt[0][2] == 'X' || ttt[1][0] == 'X' && ttt[1][1] == 'X' && ttt[1][2] == 'X' || ttt[2][0] == 'X' && ttt[2][1] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][0] == 'X' && ttt[2][0] == 'X' || ttt[0][1] == 'X' && ttt[1][1] == 'X' && ttt[2][1] == 'X' || ttt[0][2] == 'X' && ttt[1][2] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][1] == 'X' && ttt[2][2] == 'X' || ttt[2][0] == 'X' && ttt[1][1] == 'X' && ttt[0][2] == 'X'){
printf("X WINS!!!!!");
return 0;
}
could be arranged to be a function checkWin(char c)
by the way:
ttt[move.i][move.j] = ttt[move.i][move.j];
this line does nothing useful (it writes the value of an element to the same element so nothing changes) - you can simply delete this line
You should add a 'continue' after the duplicate check (see below) so that your code doesnot do all the checking of who wins after a duplicate move. I believe this is the main source of your issue. However it could be more useful if you could elaborate little more what you meant by 'sometimes it works and sometimes it dont'
if( ttt[move.i][move.j] == 'O' || ttt[move.i][move.j] == 'X' ){
printf("\n\n\nInvalid move. Try again\n\n\n");
counter = counter - 1;
continue;
}
There are some issues in your code - I will explain the one, that are not already pointed out by the other comments.
Your 'if' conditionals are very Long and confusing - you don't have to check against 'X' and 'O' respectively - you can just compare them and whatever result is hold there, it wins. However a Special check for '-' is needed, but it's easier maintainable in my opinion. However that's not the main Point here.
Your mistake was, that you checked if theres a winner BEFORE you actually set the current cell. so the winner would only appear one round to late. Consider the improved code (i also applied some of the improvements from the other comments):
#include <stdio.h>
struct coordinate{
int i;
int j;
};
//function that prints the tic-tac-toe board
int printTable( char array[3][3]){
printf(" X's first O's second\n");
printf("\n 0 1 2\n");
printf("\n | | \n");
printf(" 0 %c | %c | %c \n", array[0][0], array[0][1], array[0][2]);
printf(" ___|___|___\n");
printf(" | | \n");
printf(" 1 %c | %c | %c \n", array[1][0], array[1][1], array[1][2]);
printf(" ___|___|___\n");
printf(" | | \n");
printf(" 2 %c | %c | %c \n", array[2][0], array[2][1], array[2][2]);
printf(" | | \n\n");
return 0;
}
int main (void){
struct coordinate move;
char game;
int counter, x, y;
char ttt[3][3] = {
{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}
};
printf("\nWelcome to tic tac toe!\n");
//Checks if you want to play
printf("Would you like to play? y/n\n");
scanf("%c", &game);
//stops game by returning zero
if(game == 'n'){
printf("Have a pleasant day");
}else /*if(game == 'y')*/{ //starts game! =)
for(counter = 1; counter <= 9; counter++){
printTable(ttt);
//asks player where they want to move
printf("Where would you like to move?");
scanf("%i %i", &move.i, &move.j);
//Checks for duplicate move
if( ttt[move.i][move.j] == 'O' || ttt[move.i][move.j] == 'X' ){
printf("\n\n\nInvalid move. Try again\n\n\n");
counter = counter - 1;
continue;
}
//X's turns
if( counter % 2 == 0){
ttt[move.i][move.j] = 'X';
}else if( counter % 2 == 1 ){
ttt[move.i][move.j] = 'O';
}
/* horizontal checks */
for(x = 0; x < 3; x++){
if(ttt[x][0] != '-' && ttt[x][0] == ttt[x][1] && ttt[x][1] == ttt[x][2]){
printf("%c WINS!!!!!", ttt[x][0]);
return 0;
}
}
/* vertical checks */
for(y = 0; y < 3; y++){
if(ttt[0][y] != '-' && ttt[0][y] == ttt[1][y] && ttt[1][y] == ttt[2][y]){
printf("%c WINS!!!!!", ttt[0][y]);
return 0;
}
}
/* diagonal checks */
if(ttt[0][0] != '-' && ttt[0][0] == ttt[1][1] && ttt[1][1] == ttt[2][2]){
printf("%c WINS!!!!!", ttt[0][0]);
return 0;
}else if(ttt[0][2] != '-' && ttt[0][2] == ttt[1][1] && ttt[1][1] == ttt[2][0]){
printf("%c WINS!!!!!", ttt[0][2]);
return 0;
}
if( counter >= 9){
printf("Cat's Game =(");
return 0;
}
}
}
return 0;
}