SlideShare uma empresa Scribd logo
1 de 12
Baixar para ler offline
#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

Mais conteúdo relacionado

Semelhante a #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
 
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
 
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
 

Semelhante a #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
 

Mais de 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
 

Mais de 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
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

#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]