I'm getting the wrong output. The black screen is my output and the white screen is the right output. In my output the "V" is always in the right placement but the symbols under that line is always in the wrong placement.
: (hyphen) represents undiscovered locations
! : represents bombs
$ : represents treasure
& : represents both a bomb and treasure
. : represents a visited location that had neither a bomb nor a treasure
Check for BOTH a bomb AND a treasure
Check both the bomb and treasure member arrays to see if a value of 1 is set for both at the same
location
Update the player’s counters accordingly (bomb: reduce lives, treasure: increase treasure counter)
Display an appropriate message (use symbol: [&] to denote a bomb AND treasure, the treasure is
considered a “life insurance payout”)
If there is no bomb or treasure at the location entered by the user
Display an appropriate message (use symbol: [.] to denote nothing found)
This is my code:
do
{
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else
{
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n");
do
{
lives = 0;
printf("Next Move [1-%d]: ", gameSettings.gameLength);
scanf("%d", &playerData.currentMove);
if (playerData.currentMove >= 1 && playerData.currentMove <= gameSettings.gameLength)
{
printf("\n");
lives++;
}
else
{
printf(" Out of Range!!!\n");
}
} while (lives < 1);
if (gameSettings.revealedPosition[playerData.currentMove] == hiddenPosition)
{
if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [.] ...Nothing found here... [.]\n\n");
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [!] !!! BOOOOOM !!! [!]\n\n");
playerData.playerLives -= 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
printf("===============> [$] $$$ Found Treasure! $$$ [$]\n\n");
playerData.treasureNum += 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
printf("===============> [&] !!! BOOOOOM !!! [&]\n");
printf("===============> [&] $$$ Life Insurance Payout!!! [&]\n\n");
gameSettings.gameMoves -= 1;
playerData.treasureNum += 1;
playerData.playerLives -= 1;
}
}
else
{
printf("===============> Dope! You've been here before!\n\n");
}
} while (playerData.playerLives != 0 && gameSettings.gameMoves != 0);
printf("No more LIVES remaining!\n\n");
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else {
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n\n");
printf("##################\n# Game over! #\n##################\n\n");
printf("You should play again and try to beat your score!!\n");
return 0;
Related
I was working on learning c code and was making a tic-tac-toe game. The Boolean issue was fixed. Now the issue is that it is looping the printf("There is no empty space!"); and prinf("Invalid !!!"); after it take the player1 name. I also wanted to know if the line where I printed the array with the grid is correct or not.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
int row;
int column;
char token = 'x';
bool tie = false;
char n1[256];
char n2[256];
void functionboard()
{
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
printf(" | | \n");
printf(" ", space[0][0], "| ", space[0][1], "| ", space[0][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[1][0], " | ", space[1][1], " | ", space[1][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[2][0], " | ", space[2][1], " | ", space[2][2], " \n");
printf(" | | \n");
}
void functionOne()
{
int dight;
if (token == 'x')
{
printf(n1, "please enter");
scanf("&d", &dight);
}
if (token == '0')
{
printf(n2, "please enter");
scanf("&d", &dight);
}
if (dight == 1)
{
row = 0;
column = 0;
}
if (dight == 2)
{
row = 0;
column = 1;
}
if (dight == 3)
{
row = 0;
column = 2;
}
if (dight == 4)
{
row = 1;
column = 0;
}
if (dight == 5)
{
row = 1;
column = 1;
}
if (dight == 6)
{
row = 1;
column = 2;
}
if (dight == 7)
{
row = 2;
column = 0;
}
if (dight == 8)
{
row = 2;
column = 1;
}
if (dight == 9)
{
row = 2;
column = 2;
}
else if (dight < 1 || dight > 9)
{
prinf("Invalid !!!");
}
if (token == 'x' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = 'x';
token = '0';
}
else if (token == '0' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = '0';
token = 'x';
}
else
{
printf("There is no empty space!");
functionboard();
}
functionOne();
}
bool functionDraw()
{
for (int i = 0; i < 3; i++)
{
if (space[i][0] == space[i][1] && space[i][0] == space[i][2] || space[0][i] == space[1][i] && space[0][i] == space[2][i])
return true;
}
if (space[0][0] == space[1][1] && space[1][1] == space[2][2] || space[0][2] == space[1][1] && space[1][1] == space[2][0])
{
return true;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (space[i][j] != 'x' && space[i][j] != '0')
{
return false;
}
}
}
tie = true;
return false;
}
int main()
{
printf("Enter the name of the first player : \n");
scanf("%c", n1);
printf("Enter the name of the second player : \n");
scanf("%c", n2);
printf("%c is player1 so he/she will play first \n", n1);
printf("%c is player2 so he/she will play first \n", n2);
while (!functionDraw())
{
functionboard();
functionOne();
functionDraw();
}
if (token == 'x' && tie == false)
{
printf("%c Wins!!\n", n2);
}
else if (token == '0' && tie == false)
{
printf("%c Wins!!\n", n1);
}
else
{
printf("its a draw!!");
}
}
If the error you're getting is about the type bool and not the variable itself, see this question: you need to also include <stdbool.h> to be able to use the bool type.
There's no built-in bool type in classic C. You can use it though by using #include <stdbool.h>. The other solution is replacing it with an int since it can be used like a bool
I'm having trouble with adding power ups. I don't know where to put it. I'm a first year college; can somebody help?
This is what I did. I want to add power ups in this game where whenever I destroy enemy, it drops power ups like firing speed and double shooting.
I don't know what to put inside to have the enemy drop some power up.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int main()
{
int sizey = 23;
int sizex = 40;
int x, y, yi;
char world[sizey][sizex];
char player = 'A';
char playerLaser = '^';
char enemy = 'M';
char enemyShielded = 'O';
char enemyLaser = 'U';
char explosion = 'X';
int score = 0;
int victory = 1;
int laserReady = 1;
int enemyReady = 0;
srand(time(NULL));
/*welcome screen*/
printf("\n \n Welcome soldier! \n \n \n \n");
Sleep(1000);
printf(" Brave the COMMAND PROMPT INVADERS and come back a hero. \n \n \n \n");
Sleep(2500);
printf(" Your operating system is depending upon you. \n \n \n \n");
Sleep(2500);
printf(" Good luck.");
Sleep(1000);
printf("\n \n \n \n Press any key to start.");
getch();
/*initialise world*/
int totalEnemies = 0;
for (x = 0; x < sizex; x ++) {
for (y = 0; y < sizey; y ++) {
if ((y+1) % 2 == 0 && y < 7 && x > 4
&& x < sizex - 5 && x % 2 ==0) {
world[y][x] = enemy;
totalEnemies ++;
}
else if ((y+1) % 2 == 0 && y >= 7 && y < 9 && x > 4
&& x < sizex - 5 && x % 2 ==0){
world[y][x] = enemyShielded;
totalEnemies = totalEnemies + 2;
}
else {
world[y][x] = ' ';
}
}
}
world[sizey - 1][sizex / 2] = player;
int i = 1;
char direction = 'l';
char keyPress;
int currentEnemies = totalEnemies;
while(currentEnemies > 0 && victory) {
int drop = 0;
int enemySpeed = 1 + 10 * currentEnemies / totalEnemies;
laserReady ++;
/*display world*/
system("cls");
printf(" SCORE: %d", score);
printf("\n");
for (y = 0; y < sizey; y ++) {
printf("|");
for (x = 0; x < sizex; x ++) {
printf("%c",world[y][x]);
}
printf("|");
printf("\n");
}
/*laser time*/
for (x = 0; x < sizex; x ++) {
for (y = sizey-1; y >= 0; y --) {
if (i%2 == 0 && world[y][x] == enemyLaser
&& (world[y+1][x] != enemy & world[y+1][x] != enemyShielded)){
world[y+1][x] = enemyLaser;
world[y][x] = ' ';
}
else if (i%2 == 0 && world[y][x] == enemyLaser
&& (world[y+1][x] == enemy | world[y+1][x] == enemyShielded)){
world[y][x] = ' ';
}
}
}
for (x = 0; x < sizex; x ++) {
for (y = 0; y < sizey; y ++) {
if ((i % 5) == 0 && (world[y][x] == enemyShielded
| world[y][x] == enemy) && (rand() % 15) > 13
&& world[y+1][x] != playerLaser) {
for (yi = y+1; yi < sizey; yi ++) {
if (world[yi][x] == enemy
| world[yi][x] == enemyShielded) {
enemyReady = 0;
break;
}
enemyReady = 1;
}
if (enemyReady) {
world[y+1][x] = enemyLaser;
}
}
if (world[y][x] == playerLaser && world[y-1][x] == enemy) {
world[y][x] = ' ';
world[y-1][x] = explosion;
currentEnemies --;
score = score + 50;
}
else if (world[y][x] == playerLaser
&& world[y-1][x] == enemyShielded) {
world[y][x] = ' ';
world[y-1][x] = enemy;
currentEnemies --;
score = score + 50;
}
else if (world[y][x] == playerLaser
&& world[y-1][x] == enemyLaser) {
world[y][x] = ' ';
}
else if (world[y][x] == explosion) {
world[y][x] = ' ';
}
else if ((i+1) % 2 == 0 && world[y][x] == enemyLaser
&& world[y+1][x] == player) {
world[y+1][x] = explosion;
world[y][x] = ' ';
victory = 0;
}
else if (world[y][x] == playerLaser
&& world[y-1][x] != enemyLaser) {
world[y][x] = ' ';
world[y-1][x] = playerLaser;
}
}
}
/*update enemy direction*/
for (y = 0; y < sizey; y ++) {
if (world[y][0] == enemy) {
direction = 'r';
drop = 1;
break;
}
if (world[y][sizex-1] == enemy){
direction = 'l';
drop = 1;
break;
}
}
/*update board*/
if (i % enemySpeed == 0) {
if (direction == 'l') {
for (x = 0; x < sizex - 1; x ++) {
for (y = 0; y < sizey; y ++) {
if (drop && (world[y-1][x+1] == enemy
|| world[y-1][x+1] == enemyShielded)){
world[y][x] = world[y-1][x+1];
world[y-1][x+1] = ' ';
}
else if (!drop && (world[y][x+1] == enemy
|| world[y][x+1] == enemyShielded)) {
world[y][x] = world[y][x+1];
world[y][x+1] = ' ';
}
}
}
}
else {
for (x = sizex; x > 0; x --) {
for (y = 0; y < sizey; y ++) {
if (drop && (world[y-1][x-1] == enemy
|| world[y-1][x-1] == enemyShielded)) {
world[y][x] = world[y-1][x-1];
world[y-1][x-1] = ' ';
}
else if (!drop && (world[y][x-1] == enemy
|| world[y][x-1] == enemyShielded)) {
world[y][x] = world[y][x-1];
world[y][x-1] = ' ';
}
}
}
}
for (x = 0; x < sizex; x ++) {
if (world[sizey - 1][x] == enemy) {
victory = 0;
}
}
}
/*control player*/
if(kbhit()){
keyPress = getch();
}
else {
keyPress = ' ';
}
if (keyPress == 'a') {
for (x = 0; x < sizex; x = x+1) {
if ( world[sizey-1][x+1] == player) {
world[sizey-1][x] = player;
world[sizey-1][x+1] = ' ';
}
}
}
if (keyPress == 'd') {
for (x = sizex - 1; x > 0; x = x-1) {
if ( world[sizey-1][x-1] == player) {
world[sizey-1][x] = player;
world[sizey-1][x-1] = ' ';
}
}
}
if (keyPress == 'm' && laserReady > 2) {
for (x = 0; x < sizex; x = x+1) {
if ( world[sizey-1][x] == player) {
world[sizey - 2][x] = playerLaser;
laserReady = 0;
}
}
}
i ++;
Sleep(50);
}
system("cls");
printf(" SCORE: %d", score);
printf("\n");
for (y = 0; y < sizey; y ++) {
printf("|");
for (x = 0; x < sizex; x ++) {
printf("%c",world[y][x]);
}
printf("|");
printf("\n");
}
Sleep(1000);
system("cls");
if (victory != 0) {
printf("\n \n \n \n \n \n CONGRATULATIONS! \n \n \n \n \n");
Sleep(1000);
printf("\n \n Score: %d", score);
Sleep(1000);
int bonus = totalEnemies*20 - i;
printf("\n \n Bonus: %d", bonus);
Sleep(1000);
printf("\n \n Total Score: %d", score + bonus);
printf("\n \n \n \n Well done");
Sleep(1000);
printf(", Hero.");
Sleep(1000);
getch();
}
else {
printf("\n \n \n \n \n \n You have failed.");
Sleep(1000);
printf("\n \n \n \n \n \n Windows is doomed.");
Sleep(1000);
printf("\n \n Final Score: %d", score);
getch();
}
}
I'm currently trying to make use of the Minimax Algorithm and create a "Unbeatable Computer Player". I'm banging my head with this one for hours now and I cant seem to figure out why the "Computer" is going in order and not making the right decisions. Does anybody have an idea? maybe something sticks out for you I can't put my finger on what's wrong honestly. I bet it has something to do with the recursive call but I'm lost here.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char BOARD[9][4];
void createBoard(){
for(int i = 0;i < 9;i++){
strcpy(BOARD[i], "[]");
}
strcpy(BOARD[6], "[X]");
strcpy(BOARD[1], "[O]");
strcpy(BOARD[3], "[X]");
}
void printBoard(){
printf("\n\n");
for(int i = 0;i < 9;i++){
printf("%s", BOARD[i]);
if((i + 1) % 3 == 0){
printf("\n");
}
}
printf("\n\n");
}
void makeMove(char* player, int position){
char playerFinal[5];
strcpy(playerFinal, "[");
strcat(playerFinal, player);
strcat(playerFinal, "]");
if(strcmp(BOARD[position], "[]") == 0){
strcpy(BOARD[position], playerFinal);
}
}
bool movesLeft(){
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
return true;
}
}
return false;
}
char* isWinner(){
for(int i = 0;i < 9;i = i + 3){
if(strcmp(BOARD[i], BOARD[i + 1]) == 0 && strcmp(BOARD[i + 1], BOARD[i + 2]) == 0){
if(strcmp(BOARD[i], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[i], "[O]") == 0){
return "O";
}
}
}
for(int i = 0;i < 3;i++){
if(strcmp(BOARD[i], BOARD[i + 3]) == 0 && strcmp(BOARD[i + 3], BOARD[i + 6]) == 0){
if(strcmp(BOARD[i], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[i], "[O]") == 0){
return "O";
}
}
}
if(strcmp(BOARD[0], BOARD[4]) == 0 && strcmp(BOARD[4], BOARD[8]) == 0){
if(strcmp(BOARD[0], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[0], "[O]") == 0){
return "O";
}
}
if(strcmp(BOARD[2], BOARD[4]) == 0 && strcmp(BOARD[4], BOARD[6]) == 0){
if(strcmp(BOARD[2], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[2], "[O]") == 0){
return "O";
}
}
if(!movesLeft()){
return "tie";
}
return "";
}
int minimax(bool isMaximizing){
char* winner = isWinner();
printf("Function Called\n");
printf("%s", winner);
if(strcmp(winner, "O") == 0){
return 10;
}
if(strcmp(winner, "X") == 0){
return -10;
}
if(strcmp(winner, "tie") == 0){
return 0;
}
if(isMaximizing){
int score;
int bestScore = -1000000;
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
makeMove("O", i);
score = minimax(false);
strcpy(BOARD[i], "[]");
if(score > bestScore){
bestScore = score;
}
}
}
return bestScore;
}
else{
int score;
int bestScore = 1000000;
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
makeMove("X", i);
score = minimax(true);
strcpy(BOARD[i], "[]");
if(score < bestScore){
bestScore = score;
}
}
}
return bestScore;
}
}
void bestMove(){
int bestScore = -1000;
int position;
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
int score;
makeMove("O", i);
score = minimax(false);
strcpy(BOARD[i], "[]");
printf("Position %d\n", i);
printf("Best Score %d\n", bestScore);
printf("Score %d\n", score);
if(score > bestScore){
bestScore = score;
position = i;
}
}
}
makeMove("O", position);
}
int main(){
int position;
createBoard();
printBoard();
bool player = true;
char* winner = "";
while(strcmp(winner, "") == 0){
if(player){
printf("Its the Players Turn\nChoose a Position: ");
scanf("%d", &position);
makeMove("X", position -1);
player = false;
winner = isWinner();
}
else{
printf("Its the Computers Turn");
bestMove();
player = true;
winner = isWinner();
}
printBoard();
}
printf("The Winner is: %s\n", winner);
return 0;
}
I tried to create a 8 x 8 checkers game. I am trying to move the hyphen ' _ ' in the 2d array to select the character 'X' that I want. I have created the if statement for detecting the hyphen ' _ ' but seem that my code isn't working, I really need help. I am new to programming.
#include <stdio.h>
void gameboard(char board[8][8])
{
int x, y;
for(x=0; x<8; x++)
{
for(y=0; y<8; y++)
{
printf("=---=");
}
printf("\n\n");
for(y=0;y<8;y++)
{
printf("| %c |",board[x][y]);
}
printf("\n\n");
}
for(x=0;x<8;x++)
{
printf("=---=");
}
}
void character(char board[8][8])
{
int x,y;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
if(x<3){
if(x%2 == 0){
if(x%2 == 0){
board[x][y] = 'O';
}
if(y%2==1){
board[x][y]= ' ';
}
}
if(x%2 == 1){
if(y%2 == 0){
board[x][y] = ' ';
}
if(y%2 ==1){
board[x][y]= 'O';
}
}
}
if((x==3) || (x==4)){
board[x][y] = ' ';
}
if(x>4)
{
if(x%2 == 0){
if(y%2 == 0){
board[x][y] = 'X';
}
if(y%2 ==1){
board[x][y]= ' ';
}
}
if(x%2 == 1){
if(y%2 == 0){
board[x][y] = ' ';
}
if(y%2 ==1){
board[x][y]= 'X';
}
}
if(x==5 && y ==1)
{
if(x%2 == 1){
if(y%2 == 1){
board[x][y] = '_';
}
}
}
}
}
}
}
void playgame(char board[8][8])
{
int x=0, y=0, a, b, c=0,input;
char token;
printf("\n\n---START GAME---\n");
if(token == '_')
{
printf("Please select your token : ");
}
for(a=0; a<8; a++)
{
for(b=0; b<8; b++)
{
if(board[a][b] == token & c == 0)
{
x = a;
y = b;
c++;
}
}
}
printf("1 to go right\n");
printf("2 to go left\n");
printf("3 to go up left\n");
printf("4 to go up right\n");
printf("5 to go down left\n");
printf("6 to go down right\n");
printf("7 to select token\n");
fflush(stdin);
scanf("%i", &input);
if(input == 1)
{
board[x][y+2] = token;
y++;
}
else if(input == 2)
{
board[x][y-2] = token;
y--;
}
else if(input == 3)
{
board[x-1][y-1] = token;
x--;
y--;
}
else if(input == 4)
{
board[x-1][y+1] = token;
x--;
y++;
}
else if(input == 5)
{
board[x+1][y-1] = token;
x++;
y--;
}
else if(input == 6)
{
board[x+1][y+1] = token;
x++;
y++;
}
else
{
board[x][y] = token;
}
}
int main()
{
char bx[8][8];
gameboard(bx);
playgame(bx);
return 0;
}
You have to initialize the array, then use switch statement to calculate the new position based on input.
Save the old position and swap the content of the new cell with that of the old cell in the saved position.
int main()
{
char board[8][8];
int x, y;
for (x = 0; x < 8; x++)
for (y = 0; y < 8; y++)
board[x][y] = '.';
board[0][0] = '_';
int xpos = 0;
int ypos = 0;
while (1)
{
system("cls||clear");
//print board
for (y = 0; y < 8; y++)
{
for (x = 0; x < 8; x++)
printf("%c", board[x][y]);
printf("\n");
}
printf("Menu\n 1 left \n2 right \n3 up \n4 down \n");
int savex = xpos;
int savey = ypos;
int move = 0;
scanf("%d", &move);
char c;
while ((c = getchar()) != '\n' && c != EOF);
switch (move)
{
case 1: if (xpos > 0) xpos--; break;
case 2: if (xpos < 7) xpos++; break;
case 3: if (ypos > 0) ypos--; break;
case 4: if (ypos < 7) ypos++; break;
}
//swap position:
board[savex][savey] = '.';
board[xpos][ypos] = '_';
}
return 0;
}
In the main file, I loop through each line of input until it hits the words, then I pass the word its searching for to startSearch with puzzleArray, the solvedArray I want to get back, the word as string, size as number of rows, and length as number of columns.
Currently, I keep getting segmentation faults/or endless loops. Any help over my algorithm/code would be greatly appreciated.
void startSearch(char** puzzleArray,char** solvedArray,char* string,int size,int length)
{
char* direction = "";
int solved = 1;
int j = 0;
while( j <= 7 && solved != 0)
{
if(j == 0)
{
direction = "up";
}
else if(j == 1)
{
direction = "upRight";
}
else if(j == 2)
{
direction = "right";
}
else if(j == 3)
{
direction = "downRight";
}
else if(j == 4)
{
direction = "down";
}
else if(j == 5)
{
direction = "downLeft";
}
else if(j == 6)
{
direction = "left";
}
else if(j == 7)
{
direction = "upLeft";
}
solved = recursiveSearch(puzzleArray,solvedArray,string,direction,size,length,0,0,0);
j++;
}
}
int recursiveSearch(char** puzzleArray,char** solvedArray,char* string,char* direction,int sizeOfPuzzle,int lengthOfArrayWithSpaces,int rowPos,int colPos,int stringPosition)
{
int lengthOfWord;
int i = rowPos;
int j = colPos;
int found = 0;
int empty = 1;
char c = string[stringPosition];
int position = stringPosition;
lengthOfWord = lengthOfArray(string);
if(string[position+1] == '\0')
{
return 0;
}
while(empty != 0)
{
if(string[stringPosition] == puzzleArray[i][j])
{
found = 1;
}
else if(rowPos < sizeOfPuzzle && colPos < lengthOfArrayWithSpaces)
{
stringPosition = 0;
for(i = rowPos; i < sizeOfPuzzle && found != 1; i++)
{
for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)
{
if(string[stringPosition] == puzzleArray[i][j])
{
found = 1;
rowPos = i;
colPos = j;
stringPosition = 0;
}
}
}
if(found == 0)
{
empty = 1;
}
}
if(found == 1)
{
position = stringPosition + 1;
if(rowPos-1 >= 0)
{
//printf("\nString:%cPuzzleArray:%c",string[position],puzzleArray[rowPos-1][colPos]);
if(string[position] == puzzleArray[rowPos-1][colPos] && direction == "up")
{
//printf("UP");
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos,position+1))
{
solvedArray[rowPos-1][colPos] = puzzleArray[rowPos-1][colPos];
return 0;
}
}
else if(colPos+2 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos-1][colPos+2] && direction == "upRight")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos+2,position+1))
{
solvedArray[rowPos-1][colPos+2] = puzzleArray[rowPos-1][colPos+2];
return 0;
}
}
}
}
if(colPos+2 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos][colPos+2] && direction == "right")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos,colPos+2,position+1))
{
solvedArray[rowPos][colPos+2] = puzzleArray[rowPos][colPos+2];
return 0;
}
}
if(rowPos+1 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos+1][colPos+2] && direction == "downRight")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
{
solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
return 0;
}
}
}
}
if(rowPos+1 <= sizeOfPuzzle)
{
if(string[position] == puzzleArray[rowPos+1][colPos] && direction == "down")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos,position+1))
{
solvedArray[rowPos+1][colPos] = puzzleArray[rowPos+1][colPos];
return 0;
}
}
if(rowPos + 1 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos+1][colPos-2] && direction == "downLeft")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos-2,position+1))
{
solvedArray[rowPos+1][colPos-2] = puzzleArray[rowPos+1][colPos-2];
return 0;
}
}
}
}
if(colPos-2 >= 0)
{
if(string[position] == puzzleArray[rowPos][colPos-2] && direction == "left")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
{
solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
return 0;
}
}
if(rowPos - 1 >= 0)
{
if(string[position] == puzzleArray[rowPos-1][colPos-2] && direction == "upLeft")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos-2,position+1))
{
solvedArray[rowPos-1][colPos-2] = puzzleArray[rowPos-1][colPos-2];
return 0;
}
}
}
}
}
}
return 1;
}
direction == "up"
This is not how you compare two strings to be equal. Use strcmp / strncmp for string comparison. This kind of comparison appears all over your code.
Also:
for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)
This j < puzzleArray[rowPos][colPos] != '\0' looks dubious, what are you trying to do?