SlideShare a Scribd company logo
#include <stdio.h>
#include <stdlib.h> // malloc
#include <time.h> // srand
#include <unistd.h> // optarg
char **create_board(int board_sz);
void print_board(char **Board, int board_sz);
int is_draw(char **Board, int board_sz);
char winning_move(char **Board, int board_sz, int row_index, int col_index);
// Prints the Board
void print_board(char **Board, int board_sz)
{
printf(" ");
for (int i = 0; i < board_sz; i++)
{
printf("|%d", i);
}
printf("|n");
for (int i = 0; i < board_sz; i++)
{
printf("%c", 'a' + i);
for (int j = 0; j < board_sz; j++)
{
printf("|%c", Board[i][j]);
}
printf("|n");
}
}
// Creates nxn tic tac toe Board
char **create_board(int board_sz)
{
char **Board = (char **)malloc(board_sz * sizeof(char *));
for (int i = 0; i < board_sz; i++)
{
Board[i] = (char *)malloc(board_sz * sizeof(char));
}
for (int i = 0; i < board_sz; i++)
{
for (int j = 0; j < board_sz; j++)
{
Board[i][j] = ' ';
}
}
return Board;
}
// Returns true if the game is a draw
int is_draw(char **Board, int board_sz)
{
for (int i = 0; i < board_sz; i++)
{
for (int j = 0; j < board_sz; j++)
{
if (Board[i][j] == ' ')
{
// empty square, so game ain't over yet
return 0;
}
}
}
// no empty squares, so it's a draw
return 1;
}
// Returns 'X' if (i,j) was a winning move for X
// Returns 'O' if (i,j) was a winning move for O
// Returns ASCII value 0 otherwise
char winning_move(char **Board, int board_sz, int row_index, int col_index)
{
int win = 1;
// check row
for (int k = 0; k < board_sz; k++)
{
if (Board[row_index][k] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win) // means Board[i][k] == Board[i][j]
{
return Board[row_index][col_index];
}
// check column
win = 1;
for (int k = 0; k < board_sz; k++)
{
if (Board[k][col_index] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win)
{
return Board[row_index][col_index];
}
// check forward diagonal
win = 1;
for (int k = 0; k < board_sz; k++)
{
if (Board[k][k] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win)
{
return Board[row_index][col_index];
}
// check reverse diagonal
win = 1;
for (int k = 0; k < board_sz; k++)
{
if (Board[k][board_sz - k - 1] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win)
{
return Board[row_index][col_index];
}
// got nothing
return 0;
}
// -------------------------getMove -------------------------------
int getMove( char ** Board, int board_sz, char *x, char *y ){
Board = create_board(board_sz);
char winner = '0';
char row = *x;
char col = *y;
char turn = 'X';
char ch;
int computer_enabled = 0;
if (turn == 'X' || !computer_enabled)
{
printf("computer 'O' Moves are 'enabled'n");
printf("-Player's %c turn (qq to quit)nn", turn);
// suggestion
do
{
row = rand() % board_sz + 'a'; //
col = rand() % board_sz + '0';
} while (Board[row - 'a'][col - '1'] != ' ');
printf("*---> suggestion:t(%c %c)nn", row, col);
printf("(X) Enter Move (row column) ---------------------------> ");
fflush(stdout);
scanf(" %c %c", &row, &col);
// quit when enter qq.
if (row == 'q' && col == 'q'){
exit(0);
}
}
else
{
printf("computer 'O' Moves are 'enabled'n");
printf("*Player's O turn (qq to quit)nnn");
// Randomly pick a move
do
{
row = rand() % board_sz + 'a'; //
col = rand() % board_sz + '0';
} while (Board[row - 'a'][col - '1'] != ' ');
printf("(O) computer Picks (%c %c) (hit a key to continue) ----> ", row, col);
}
// Make move if square is free
int rowind = row - 'a';
int colind = col - '0';
if (rowind >= board_sz || colind >= board_sz)
{
printf("Invalid moven");
}
else if (Board[rowind][colind] == ' ')
{
char enter;
enter = getchar();
printf("Move is %c %c (%d, %d)n", row, col, rowind, colind);
Board[rowind][colind] = turn;
if (turn == 'X')
{
turn = 'O';
}
else
{
turn = 'X';
}
winner = winning_move(Board, board_sz, rowind, colind);
}
else
{
printf("Square is occupied; try again.n");
}
// Game over - print Board & declare finish
print_board(Board, board_sz);
if (winner == 'X' || winner == 'O')
{
printf("Congratulations %c!n", winner);
}
else
{
printf("Game ends in a draw.n");
}
// Free memory
for (int i = 0; i < board_sz; i++)
{
free(Board[i]);
}
free(Board);
return 0;
}
// -----------------------------------------------------------------------
// main function where program start
int main(int argc, char **argv)
{
int opt;
int board_sz = 3;
int computer_enabled = 0;
srand(time(NULL));
// Parse command line arguments using getopt()
while ((opt = getopt(argc, argv, "s:i")) != -1)
{
switch (opt)
{
case 's':
board_sz = atoi(optarg);
break;
case 'i':
computer_enabled = 1;
break;
default:
break;
}
}
char **Board = create_board(board_sz);
char winner = '0';
char *row;
char *col;
char turn = 'X';
char ch;
// standard game loop
while (!winner && !is_draw(Board, board_sz))
{
print_board(Board, board_sz);
getMove(Board, board_sz, *row, *col);
return 0;
}
}
this is tic-tac-toe game code in c.
but i got a error here.
could you please fix this code and explain why the error occurs here?
please fix the code and show the run result also. thank you.
C split_copy.c 2 incompatible integer to pointer conversion passing 'char' to parameter of type
'char *'; remove * [-Wint-conversion] gcc [Ln 273, Col 34] incompatible integer to pointer
conversion passing 'char' to parameter of type 'char ; remove [ Wint-conversion] gcc [Ln 273,
Col 40]
#include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf

More Related Content

Similar to #include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf

#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
singhanubhav1234
 
include ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdfinclude ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdf
adisainternational
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
armcomputers
 
i need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docxi need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docx
ursabrooks36447
 
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdfinclude ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
adisainternational
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
calderoncasto9163
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
Ankit Gupta
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
aravlitraders2012
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
poblettesedanoree498
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
Connect 4
Connect 4Connect 4
Connect 4
Ankit Gupta
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
fonecomp
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
anithareadymade
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding class
Jonah Marrs
 

Similar to #include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf (15)

#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
 
include ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdfinclude ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdf
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
 
i need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docxi need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docx
 
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdfinclude ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
Connect 4
Connect 4Connect 4
Connect 4
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding class
 

More from JacobQfDNolanr

(2) Find the exact solution of the following recurrence- Show your ans.pdf
(2) Find the exact solution of the following recurrence- Show your ans.pdf(2) Find the exact solution of the following recurrence- Show your ans.pdf
(2) Find the exact solution of the following recurrence- Show your ans.pdf
JacobQfDNolanr
 
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
JacobQfDNolanr
 
(a) Draw the component architecture of database system environment- Il.pdf
(a) Draw the component architecture of database system environment- Il.pdf(a) Draw the component architecture of database system environment- Il.pdf
(a) Draw the component architecture of database system environment- Il.pdf
JacobQfDNolanr
 
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
JacobQfDNolanr
 
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
JacobQfDNolanr
 
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
JacobQfDNolanr
 
(1-) Define the term 'positive control- what are some examples of 'fal.pdf
(1-) Define the term 'positive control- what are some examples of 'fal.pdf(1-) Define the term 'positive control- what are some examples of 'fal.pdf
(1-) Define the term 'positive control- what are some examples of 'fal.pdf
JacobQfDNolanr
 
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
JacobQfDNolanr
 
-What is the purpose of Class Design- - In what ways are classes refin.pdf
-What is the purpose of Class Design- - In what ways are classes refin.pdf-What is the purpose of Class Design- - In what ways are classes refin.pdf
-What is the purpose of Class Design- - In what ways are classes refin.pdf
JacobQfDNolanr
 
-Select- Question 3 all of these choices are correct- Water dissolves.pdf
-Select- Question 3 all of these choices are correct- Water dissolves.pdf-Select- Question 3 all of these choices are correct- Water dissolves.pdf
-Select- Question 3 all of these choices are correct- Water dissolves.pdf
JacobQfDNolanr
 
-One word answer- The original motivational theory - -Theory X and Y i.pdf
-One word answer- The original motivational theory - -Theory X and Y i.pdf-One word answer- The original motivational theory - -Theory X and Y i.pdf
-One word answer- The original motivational theory - -Theory X and Y i.pdf
JacobQfDNolanr
 
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
JacobQfDNolanr
 
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
JacobQfDNolanr
 
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
JacobQfDNolanr
 
-12-13 (Count characters- words- and lines in a file) Write a program.pdf
-12-13 (Count characters- words- and lines in a file) Write a program.pdf-12-13 (Count characters- words- and lines in a file) Write a program.pdf
-12-13 (Count characters- words- and lines in a file) Write a program.pdf
JacobQfDNolanr
 
--NO COPY PAST PLEASE-- Critical National Information Infrastructure.pdf
--NO COPY PAST PLEASE--  Critical National Information Infrastructure.pdf--NO COPY PAST PLEASE--  Critical National Information Infrastructure.pdf
--NO COPY PAST PLEASE-- Critical National Information Infrastructure.pdf
JacobQfDNolanr
 
--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf
--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
JacobQfDNolanr
 
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
JacobQfDNolanr
 
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
JacobQfDNolanr
 
- A scientist is studying stem length in wheat plants- She crosses two.pdf
- A scientist is studying stem length in wheat plants- She crosses two.pdf- A scientist is studying stem length in wheat plants- She crosses two.pdf
- A scientist is studying stem length in wheat plants- She crosses two.pdf
JacobQfDNolanr
 

More from JacobQfDNolanr (20)

(2) Find the exact solution of the following recurrence- Show your ans.pdf
(2) Find the exact solution of the following recurrence- Show your ans.pdf(2) Find the exact solution of the following recurrence- Show your ans.pdf
(2) Find the exact solution of the following recurrence- Show your ans.pdf
 
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
 
(a) Draw the component architecture of database system environment- Il.pdf
(a) Draw the component architecture of database system environment- Il.pdf(a) Draw the component architecture of database system environment- Il.pdf
(a) Draw the component architecture of database system environment- Il.pdf
 
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
 
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
 
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
 
(1-) Define the term 'positive control- what are some examples of 'fal.pdf
(1-) Define the term 'positive control- what are some examples of 'fal.pdf(1-) Define the term 'positive control- what are some examples of 'fal.pdf
(1-) Define the term 'positive control- what are some examples of 'fal.pdf
 
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
 
-What is the purpose of Class Design- - In what ways are classes refin.pdf
-What is the purpose of Class Design- - In what ways are classes refin.pdf-What is the purpose of Class Design- - In what ways are classes refin.pdf
-What is the purpose of Class Design- - In what ways are classes refin.pdf
 
-Select- Question 3 all of these choices are correct- Water dissolves.pdf
-Select- Question 3 all of these choices are correct- Water dissolves.pdf-Select- Question 3 all of these choices are correct- Water dissolves.pdf
-Select- Question 3 all of these choices are correct- Water dissolves.pdf
 
-One word answer- The original motivational theory - -Theory X and Y i.pdf
-One word answer- The original motivational theory - -Theory X and Y i.pdf-One word answer- The original motivational theory - -Theory X and Y i.pdf
-One word answer- The original motivational theory - -Theory X and Y i.pdf
 
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
 
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
 
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
 
-12-13 (Count characters- words- and lines in a file) Write a program.pdf
-12-13 (Count characters- words- and lines in a file) Write a program.pdf-12-13 (Count characters- words- and lines in a file) Write a program.pdf
-12-13 (Count characters- words- and lines in a file) Write a program.pdf
 
--NO COPY PAST PLEASE-- Critical National Information Infrastructure.pdf
--NO COPY PAST PLEASE--  Critical National Information Infrastructure.pdf--NO COPY PAST PLEASE--  Critical National Information Infrastructure.pdf
--NO COPY PAST PLEASE-- Critical National Information Infrastructure.pdf
 
--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf
--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
 
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
 
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
 
- A scientist is studying stem length in wheat plants- She crosses two.pdf
- A scientist is studying stem length in wheat plants- She crosses two.pdf- A scientist is studying stem length in wheat plants- She crosses two.pdf
- A scientist is studying stem length in wheat plants- She crosses two.pdf
 

Recently uploaded

Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
nitinpv4ai
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 

Recently uploaded (20)

Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 

#include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf

  • 1. #include <stdio.h> #include <stdlib.h> // malloc #include <time.h> // srand #include <unistd.h> // optarg char **create_board(int board_sz); void print_board(char **Board, int board_sz); int is_draw(char **Board, int board_sz); char winning_move(char **Board, int board_sz, int row_index, int col_index); // Prints the Board void print_board(char **Board, int board_sz) { printf(" "); for (int i = 0; i < board_sz; i++) { printf("|%d", i); } printf("|n"); for (int i = 0; i < board_sz; i++) { printf("%c", 'a' + i); for (int j = 0; j < board_sz; j++) { printf("|%c", Board[i][j]);
  • 2. } printf("|n"); } } // Creates nxn tic tac toe Board char **create_board(int board_sz) { char **Board = (char **)malloc(board_sz * sizeof(char *)); for (int i = 0; i < board_sz; i++) { Board[i] = (char *)malloc(board_sz * sizeof(char)); } for (int i = 0; i < board_sz; i++) { for (int j = 0; j < board_sz; j++) { Board[i][j] = ' '; } } return Board; } // Returns true if the game is a draw int is_draw(char **Board, int board_sz)
  • 3. { for (int i = 0; i < board_sz; i++) { for (int j = 0; j < board_sz; j++) { if (Board[i][j] == ' ') { // empty square, so game ain't over yet return 0; } } } // no empty squares, so it's a draw return 1; } // Returns 'X' if (i,j) was a winning move for X // Returns 'O' if (i,j) was a winning move for O // Returns ASCII value 0 otherwise char winning_move(char **Board, int board_sz, int row_index, int col_index) { int win = 1; // check row for (int k = 0; k < board_sz; k++)
  • 4. { if (Board[row_index][k] != Board[row_index][col_index]) { win = 0; break; } } if (win) // means Board[i][k] == Board[i][j] { return Board[row_index][col_index]; } // check column win = 1; for (int k = 0; k < board_sz; k++) { if (Board[k][col_index] != Board[row_index][col_index]) { win = 0; break; } } if (win) {
  • 5. return Board[row_index][col_index]; } // check forward diagonal win = 1; for (int k = 0; k < board_sz; k++) { if (Board[k][k] != Board[row_index][col_index]) { win = 0; break; } } if (win) { return Board[row_index][col_index]; } // check reverse diagonal win = 1; for (int k = 0; k < board_sz; k++) { if (Board[k][board_sz - k - 1] != Board[row_index][col_index]) { win = 0;
  • 6. break; } } if (win) { return Board[row_index][col_index]; } // got nothing return 0; } // -------------------------getMove ------------------------------- int getMove( char ** Board, int board_sz, char *x, char *y ){ Board = create_board(board_sz); char winner = '0'; char row = *x; char col = *y; char turn = 'X'; char ch; int computer_enabled = 0; if (turn == 'X' || !computer_enabled) { printf("computer 'O' Moves are 'enabled'n"); printf("-Player's %c turn (qq to quit)nn", turn);
  • 7. // suggestion do { row = rand() % board_sz + 'a'; // col = rand() % board_sz + '0'; } while (Board[row - 'a'][col - '1'] != ' '); printf("*---> suggestion:t(%c %c)nn", row, col); printf("(X) Enter Move (row column) ---------------------------> "); fflush(stdout); scanf(" %c %c", &row, &col); // quit when enter qq. if (row == 'q' && col == 'q'){ exit(0); } } else { printf("computer 'O' Moves are 'enabled'n"); printf("*Player's O turn (qq to quit)nnn"); // Randomly pick a move do { row = rand() % board_sz + 'a'; //
  • 8. col = rand() % board_sz + '0'; } while (Board[row - 'a'][col - '1'] != ' '); printf("(O) computer Picks (%c %c) (hit a key to continue) ----> ", row, col); } // Make move if square is free int rowind = row - 'a'; int colind = col - '0'; if (rowind >= board_sz || colind >= board_sz) { printf("Invalid moven"); } else if (Board[rowind][colind] == ' ') { char enter; enter = getchar(); printf("Move is %c %c (%d, %d)n", row, col, rowind, colind); Board[rowind][colind] = turn; if (turn == 'X') { turn = 'O'; } else {
  • 9. turn = 'X'; } winner = winning_move(Board, board_sz, rowind, colind); } else { printf("Square is occupied; try again.n"); } // Game over - print Board & declare finish print_board(Board, board_sz); if (winner == 'X' || winner == 'O') { printf("Congratulations %c!n", winner); } else { printf("Game ends in a draw.n"); } // Free memory for (int i = 0; i < board_sz; i++) { free(Board[i]); }
  • 10. free(Board); return 0; } // ----------------------------------------------------------------------- // main function where program start int main(int argc, char **argv) { int opt; int board_sz = 3; int computer_enabled = 0; srand(time(NULL)); // Parse command line arguments using getopt() while ((opt = getopt(argc, argv, "s:i")) != -1) { switch (opt) { case 's': board_sz = atoi(optarg); break; case 'i': computer_enabled = 1; break; default:
  • 11. break; } } char **Board = create_board(board_sz); char winner = '0'; char *row; char *col; char turn = 'X'; char ch; // standard game loop while (!winner && !is_draw(Board, board_sz)) { print_board(Board, board_sz); getMove(Board, board_sz, *row, *col); return 0; } } this is tic-tac-toe game code in c. but i got a error here. could you please fix this code and explain why the error occurs here? please fix the code and show the run result also. thank you. C split_copy.c 2 incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; remove * [-Wint-conversion] gcc [Ln 273, Col 34] incompatible integer to pointer conversion passing 'char' to parameter of type 'char ; remove [ Wint-conversion] gcc [Ln 273, Col 40]