Tic tac toe功能问题 [英] Tic tac toe problem with functions

查看:80
本文介绍了Tic tac toe功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!!我正在使用类进行tic tac toe游戏,我完全用它完成但是使用保存游戏和加载游戏功能存在问题我在保存游戏中遇到错误,我只想用二进制文件保存游戏使用文件i / o和我读到它,但我找不到足够的关于它!我想破坏你宝贵的一些时间,(不要介意)!!我被困在需要紧急帮助!!这就是我做的事情



我尝试过的事情:



 #include< iostream> 
#include< fstream>



//#include< cstdlib>
using namespace std;
class tic
{
private:
static char table []; //静态变量启动要求所以最后我启动kiya hua hay class say bahar
int i,选择; //局部变量jo code me istimaal hon gay
char spot;
静态字符重放; //相同oper wala case
static int player; //相同oper wala case



 void board()// board bana rahy hein jahan pey hum isko khelna chahty hein 

 cout<< \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ ttt Tac Toe \\\\\\\ n 
cout<< \t \t \t球员1(X),球员2(O)\ n \\ n;
cout<< \t \t \t<<表[1]<< | <<表[2]<< | <<表[3]<< endl; //数组ki位置零支付jo值hogi usko插入kar day ga类似agay bhi



 void Save_Game(char board [3] [3])
{
int i;
FILE * savegame = fopen(save.bin,w);
for(i = 0; i< 3; i ++)
{
fprintf(savegame,%c%c%c\ n,board [i] [0], board [i] [1],board [i] [2]);
}
fclose(savegame);
cout<< 游戏已保存;
}



 int win()
{
if(table [1] = = table [2]&& table [2] == table [3])
返回1;

else if(table [4] == table [5]&& table [5] == table [6])
return 1;

//////////

解决方案

首先检查您在文本中创建的文件或十六进制编辑器,并希望看到你写的确切内容。

幸运的是,它应该是这样的:

 XO。 
O X O
X. O

但如果没有,那么你需要确切看看为什么。

接下来要做的是看看你如何阅读文件:你不要显示,但要记住,因为你为文件写了空格和换行符,当你读回文件时它们会出现,你将不得不删除并处理它们。



并且不要使用默认文件夹来保存游戏:如果它已经投入生产,该文件夹将在Program Files中,并且严格限制访问该文件夹以减少病毒活动。如果可能,请使用与用户相关且具有相关用户的读/写权限的文件夹。


使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


hey !! i am doing tic tac toe game using classes and i am completely done with it but there is problem with it using the save game and load game function i am getting error in save game , i just want to save game in binary file using file i/o and i read about it, but i couldn't found enough about it !! i want to ruin some of your precious time, (Don't Mind) !! i am stuck in it need urgent help !!here is what i do

What I have tried:

#include<iostream>
#include<fstream>



//#include<cstdlib>
using namespace std;
class tic
{
private:
	static char table[];//static variable initilization require so last me initilize kiya hua hay class say bahar
	int i, choice;//local variables jo code me istimaal hon gay
	char spot;
	static char replay;//same oper wala case
	static int player;//same oper wala case


void board()//board bana rahy hein jahan pey hum isko khelna chahty hein

cout << "\t \t \t \t  Tic Tac Toe\t\t\n\n";
cout << "\t \t \t PLAYER 1(X)  ,  PLAYER 2(O)\n\n";
cout << "\t \t \t " << table[1] << "|" << table[2] << "|" << table[3] << endl;//array ki location zero pay jo value hogi usko insert kar day ga similarly agay bhi


void Save_Game(char board[3][3])
{
    int i;
    FILE *savegame = fopen("save.bin", "w");
    for (i = 0; i < 3; i++)
    {
        fprintf(savegame, "%c %c %c\n", board[i][0], board[i][1], board[i][2]);
    }
    fclose(savegame);
    cout << "game saved";
}


int win()
{
    if (table[1] == table[2] && table[2] == table[3])
        return 1;

    else if (table[4] == table[5] && table[5] == table[6])
        return 1;

//////////

解决方案

Start by examining the file you have created in a text or hex editor and looking to see exactly what you have written.
With luck, it should look like this:

X O .
O X O
X . O

But if it doesn't, then you need to look at exactly why.
The next thing to do is to look at how you read the file back in: you don't show that, but do remember that since you write spaces and newlines to the file, they will be present when you read the file back, and you will have to remove and cope with those.

And don't use the default folder for save games: if this ever got into production, the folder would be in Program Files and access to that is severely restricted to reduce virus activity. Use a folder that is related to the user if possible and that has read/write permissions for the relevant users.


Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于Tic tac toe功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆